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:
Ingo Schommer 2012-09-12 01:52:57 -07:00
commit 04bd8710f3

View File

@ -172,6 +172,11 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
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".
* 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) {
if(get_parent_class($class) == 'DataObject') {
if(!isset(self::$classname_spec_cache[$class])) {
$classNames = ClassInfo::subclassesFor($class);
$db = DB::getConn();
$existing = $db->hasField($class, 'ClassName') ? $db->query("SELECT DISTINCT \"ClassName\" FROM \"$class\"")->column() : array();
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 (
array (
'ClassName' => "Enum('" . implode(', ', array_unique(array_merge($existing, ClassInfo::subclassesFor($class)))) . "')",
'ClassName' => self::$classname_spec_cache[$class],
'Created' => 'SS_Datetime',
'LastEdited' => 'SS_Datetime'
),