mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
NEW Add sort columns to DB index automatically
This commit is contained in:
parent
eecbe0c7c1
commit
2e43780a8a
@ -50,6 +50,9 @@ In order to use more database specific or complex index notations, we also suppo
|
||||
);
|
||||
}
|
||||
|
||||
As of 3.7.0 `default_sort` fields will automatically become database indexes as this provides significant performance
|
||||
benefits.
|
||||
|
||||
## API Documentation
|
||||
|
||||
* [api:DataObject]
|
||||
|
@ -3387,6 +3387,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
||||
public function databaseIndexes() {
|
||||
$has_one = $this->uninherited('has_one',true);
|
||||
$classIndexes = $this->uninherited('indexes',true);
|
||||
$sort = $this->uninherited('default_sort',true);
|
||||
//$fileIndexes = $this->uninherited('fileIndexes', true);
|
||||
|
||||
$indexes = array();
|
||||
@ -3403,6 +3404,29 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
||||
}
|
||||
}
|
||||
|
||||
if ($sort && is_string($sort)) {
|
||||
$sort = preg_split('/,(?![^()]*+\\))/', $sort);
|
||||
|
||||
foreach ($sort as $value) {
|
||||
$value = trim($value);
|
||||
if(strpos($value, ' ') !== false) {
|
||||
$parts = explode(' ', $value, 2);
|
||||
$column = $parts[0];
|
||||
} else {
|
||||
$column = $value;
|
||||
}
|
||||
if (substr($column, 0, 1) === '"') {
|
||||
$column = substr($column, 1, strlen($column)-1);
|
||||
}
|
||||
if (substr($column, -1, 1) === '"') {
|
||||
$column = substr($column, 0, -1);
|
||||
}
|
||||
if ($this->hasOwnTableDatabaseField($column) && !array_key_exists($column, $indexes)) {
|
||||
$indexes[$column] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(get_parent_class($this) == "DataObject") {
|
||||
$indexes['ClassName'] = true;
|
||||
}
|
||||
|
@ -172,6 +172,45 @@ class DataObjectSchemaGenerationTest extends SapphireTest {
|
||||
$item1->delete();
|
||||
$item2->delete();
|
||||
}
|
||||
|
||||
public function testSortFieldBecomeIndexes()
|
||||
{
|
||||
$object = new DataObjectSchemaGenerationTest_Sorted();
|
||||
$indexes = $object->databaseIndexes();
|
||||
$this->assertArrayHasKey('Sort', $indexes);
|
||||
$this->assertTrue($indexes['Sort']);
|
||||
|
||||
Config::inst()->update('DataObjectSchemaGenerationTest_Sorted', 'default_sort', 'Sort ASC');
|
||||
$indexes = $object->databaseIndexes();
|
||||
$this->assertArrayHasKey('Sort', $indexes);
|
||||
$this->assertTrue($indexes['Sort']);
|
||||
|
||||
Config::inst()->update('DataObjectSchemaGenerationTest_Sorted', 'default_sort', 'Sort DESC');
|
||||
$indexes = $object->databaseIndexes();
|
||||
$this->assertArrayHasKey('Sort', $indexes);
|
||||
$this->assertTrue($indexes['Sort']);
|
||||
|
||||
Config::inst()->update('DataObjectSchemaGenerationTest_Sorted', 'default_sort', '"Sort" DESC');
|
||||
$indexes = $object->databaseIndexes();
|
||||
$this->assertArrayHasKey('Sort', $indexes);
|
||||
$this->assertTrue($indexes['Sort']);
|
||||
|
||||
Config::inst()->update('DataObjectSchemaGenerationTest_Sorted', 'default_sort', '"Sort" DESC, "Title" ASC');
|
||||
$indexes = $object->databaseIndexes();
|
||||
$this->assertArrayHasKey('Sort', $indexes);
|
||||
$this->assertTrue($indexes['Sort']);
|
||||
$this->assertArrayHasKey('Title', $indexes);
|
||||
$this->assertTrue($indexes['Title']);
|
||||
|
||||
// make sure that specific indexes aren't overwritten
|
||||
Config::inst()->update('DataObjectSchemaGenerationTest_Sorted', 'indexes', array(
|
||||
'Sort' => 'unique',
|
||||
));
|
||||
|
||||
$indexes = $object->databaseIndexes();
|
||||
$this->assertArrayHasKey('Sort', $indexes);
|
||||
$this->assertEquals('unique', $indexes['Sort']);
|
||||
}
|
||||
}
|
||||
|
||||
class DataObjectSchemaGenerationTest_DO extends DataObject implements TestOnly {
|
||||
@ -212,3 +251,12 @@ class DataObjectSchemaGenerationTest_IndexDO extends DataObjectSchemaGenerationT
|
||||
'SearchFields' => 'fulltext ("Title","Content")'
|
||||
);
|
||||
}
|
||||
|
||||
class DataObjectSchemaGenerationTest_Sorted extends DataObject implements TestOnly {
|
||||
private static $db = array(
|
||||
'Title' => 'Varchar',
|
||||
'Sort' => 'Int',
|
||||
);
|
||||
|
||||
private static $default_sort = "Sort";
|
||||
}
|
Loading…
Reference in New Issue
Block a user