DBIndexable::getIndexSpecs is responsible for returning a DBFields full indexable spec

This commit is contained in:
Robbie Averill 2017-07-18 15:03:56 +12:00
parent bd5782adca
commit fb18e441a7
4 changed files with 26 additions and 10 deletions

View File

@ -525,11 +525,8 @@ class DataObjectSchema
foreach ($fieldSpecs as $field => $spec) { foreach ($fieldSpecs as $field => $spec) {
/** @var DBField $fieldObj */ /** @var DBField $fieldObj */
$fieldObj = Injector::inst()->create($spec, $field); $fieldObj = Injector::inst()->create($spec, $field);
if ($type = $fieldObj->getIndexType()) { if ($indexSpecs = $fieldObj->getIndexSpecs()) {
$this->defaultDatabaseIndexes[$class][$field] = [ $this->defaultDatabaseIndexes[$class][$field] = $indexSpecs;
'type' => $type,
'columns' => $fieldObj->getIndexSpecs(),
];
} }
} }
return $this->defaultDatabaseIndexes[$class]; return $this->defaultDatabaseIndexes[$class];

View File

@ -296,8 +296,15 @@ abstract class DBComposite extends DBField
public function getIndexSpecs() public function getIndexSpecs()
{ {
return array_map(function ($name) { if ($type = $this->getIndexType()) {
$columns = array_map(function ($name) {
return $this->getName() . $name; return $this->getName() . $name;
}, array_keys((array) static::config()->get('composite_db'))); }, array_keys((array) static::config()->get('composite_db')));
return [
'type' => $type,
'columns' => $columns,
];
}
} }
} }

View File

@ -635,6 +635,11 @@ DBG;
public function getIndexSpecs() public function getIndexSpecs()
{ {
return [$this->getName()]; if ($type = $this->getIndexType()) {
return [
'type' => $type,
'columns' => [$this->getName()],
];
}
} }
} }

View File

@ -42,7 +42,14 @@ interface DBIndexable
public function getIndexType(); public function getIndexType();
/** /**
* Get the field(s) that should be used if this instance is indexed * Returns the index specifications for the field instance, for example:
*
* <code>
* [
* 'type' => 'unique',
* 'columns' => ['FieldName']
* ]
* </code>
* *
* @return array * @return array
*/ */