Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

FIX Cache ClassName enum so we dont query DB every call

  • Loading branch information...
commit df5a9e087d27a4e519b302e24a3225c16b202bde 1 parent f49439c
@hafriedlander hafriedlander authored
Showing with 17 additions and 3 deletions.
  1. +17 −3 model/DataObject.php
View
20 model/DataObject.php
@@ -173,6 +173,11 @@ static function set_validation_enabled($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 @@ static function set_validation_enabled($enable) {
*/
public static function database_fields($class) {
if(get_parent_class($class) == 'DataObject') {
- $db = DB::getConn();
- $existing = $db->hasField($class, 'ClassName') ? $db->query("SELECT DISTINCT \"ClassName\" FROM \"$class\"")->column() : array();
+ if(!isset(self::$classname_spec_cache[$class])) {
+ $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 (
array (
- 'ClassName' => "Enum('" . implode(', ', array_unique(array_merge($existing, ClassInfo::subclassesFor($class)))) . "')",
+ 'ClassName' => self::$classname_spec_cache[$class],
'Created' => 'SS_Datetime',
'LastEdited' => 'SS_Datetime'
),

0 comments on commit df5a9e0

Please sign in to comment.
Something went wrong with that request. Please try again.