mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Merge pull request #788 from silverstripe-rebelalliance/open/5971-perffix
FIX Cache ClassName enum so we dont query DB every call
This commit is contained in:
commit
04bd8710f3
@ -172,6 +172,11 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
|||||||
self::$validation_enabled = (bool) $enable;
|
self::$validation_enabled = (bool) $enable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var [string] - class => ClassName field definition cache for self::database_fields
|
||||||
|
*/
|
||||||
|
private static $classname_spec_cache = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the complete map of fields on this object, including "Created", "LastEdited" and "ClassName".
|
* Return the complete map of fields on this object, including "Created", "LastEdited" and "ClassName".
|
||||||
* See {@link custom_database_fields()} for a getter that excludes these "base fields".
|
* See {@link custom_database_fields()} for a getter that excludes these "base fields".
|
||||||
@ -181,12 +186,21 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
|||||||
*/
|
*/
|
||||||
public static function database_fields($class) {
|
public static function database_fields($class) {
|
||||||
if(get_parent_class($class) == 'DataObject') {
|
if(get_parent_class($class) == 'DataObject') {
|
||||||
$db = DB::getConn();
|
if(!isset(self::$classname_spec_cache[$class])) {
|
||||||
$existing = $db->hasField($class, 'ClassName') ? $db->query("SELECT DISTINCT \"ClassName\" FROM \"$class\"")->column() : array();
|
$classNames = ClassInfo::subclassesFor($class);
|
||||||
|
|
||||||
|
$db = DB::getConn();
|
||||||
|
if($db->hasField($class, 'ClassName')) {
|
||||||
|
$existing = $db->query("SELECT DISTINCT \"ClassName\" FROM \"$class\"")->column();
|
||||||
|
$classNames = array_unique(array_merge($existing, $classNames));
|
||||||
|
}
|
||||||
|
|
||||||
|
self::$classname_spec_cache[$class] = "Enum('" . implode(', ', $classNames) . "')";
|
||||||
|
}
|
||||||
|
|
||||||
return array_merge (
|
return array_merge (
|
||||||
array (
|
array (
|
||||||
'ClassName' => "Enum('" . implode(', ', array_unique(array_merge($existing, ClassInfo::subclassesFor($class)))) . "')",
|
'ClassName' => self::$classname_spec_cache[$class],
|
||||||
'Created' => 'SS_Datetime',
|
'Created' => 'SS_Datetime',
|
||||||
'LastEdited' => 'SS_Datetime'
|
'LastEdited' => 'SS_Datetime'
|
||||||
),
|
),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user