Skip to content
This repository
Browse code

FIX Cache ClassName enum so we dont query DB every call

  • Loading branch information...
commit df5a9e087d27a4e519b302e24a3225c16b202bde 1 parent f49439c
Hamish Friedlander authored September 12, 2012

Showing 1 changed file with 17 additions and 3 deletions. Show diff stats Hide diff stats

  1. 20  model/DataObject.php
20  model/DataObject.php
@@ -173,6 +173,11 @@ static function set_validation_enabled($enable) {
173 173
 	}
174 174
 
175 175
 	/**
  176
+	 * @var [string] - class => ClassName field definition cache for self::database_fields
  177
+	 */
  178
+	private static $classname_spec_cache = array();
  179
+
  180
+	/**
176 181
 	 * Return the complete map of fields on this object, including "Created", "LastEdited" and "ClassName".
177 182
 	 * See {@link custom_database_fields()} for a getter that excludes these "base fields".
178 183
 	 *
@@ -181,12 +186,21 @@ static function set_validation_enabled($enable) {
181 186
 	 */
182 187
 	public static function database_fields($class) {
183 188
 		if(get_parent_class($class) == 'DataObject') {
184  
-			$db = DB::getConn();
185  
-			$existing = $db->hasField($class, 'ClassName') ? $db->query("SELECT DISTINCT \"ClassName\" FROM \"$class\"")->column() : array();
  189
+			if(!isset(self::$classname_spec_cache[$class])) {
  190
+				$classNames = ClassInfo::subclassesFor($class);
  191
+
  192
+				$db = DB::getConn();
  193
+				if($db->hasField($class, 'ClassName')) {
  194
+					$existing = $db->query("SELECT DISTINCT \"ClassName\" FROM \"$class\"")->column();
  195
+					$classNames = array_unique(array_merge($existing, $classNames));
  196
+				}
  197
+
  198
+				self::$classname_spec_cache[$class] = "Enum('" . implode(', ', $classNames) . "')";
  199
+			}
186 200
 
187 201
 			return array_merge (
188 202
 				array (
189  
-					'ClassName'  => "Enum('" . implode(', ', array_unique(array_merge($existing, ClassInfo::subclassesFor($class)))) . "')",
  203
+					'ClassName'  => self::$classname_spec_cache[$class],
190 204
 					'Created'    => 'SS_Datetime',
191 205
 					'LastEdited' => 'SS_Datetime'
192 206
 				),

0 notes on commit df5a9e0

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