Permalink
Browse files

MINOR Caching DataObject->fieldLabels() in memory, to avoid _t() bein…

…g called excessively through fieldLabel()
  • Loading branch information...
1 parent 3002598 commit b81863cfd91fd5c482b53358929bed70419dbe60 @chillu chillu committed Apr 13, 2012
Showing with 33 additions and 25 deletions.
  1. +33 −25 model/DataObject.php
View
@@ -148,6 +148,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
protected static $_cache_get_one;
protected static $_cache_get_class_ancestry;
protected static $_cache_composite_fields = array();
+ protected static $_cache_field_labels = array();
/**
* Non-static relationship cache, indexed by component name.
@@ -2672,6 +2673,7 @@ static function reset() {
DataObject::$_cache_get_one = array();
DataObject::$_cache_composite_fields = array();
DataObject::$_cache_get_class_ancestry = array();
+ DataObject::$_cache_field_labels = array();
}
/**
@@ -2953,33 +2955,39 @@ public function searchableFields() {
* @return array|string Array of all element labels if no argument given, otherwise the label of the field
*/
public function fieldLabels($includerelations = true) {
- $customLabels = $this->stat('field_labels');
- $autoLabels = array();
-
- // get all translated static properties as defined in i18nCollectStatics()
- $ancestry = ClassInfo::ancestry($this->class);
- $ancestry = array_reverse($ancestry);
- if($ancestry) foreach($ancestry as $ancestorClass) {
- if($ancestorClass == 'ViewableData') break;
- $types = array(
- 'db' => (array)Config::inst()->get($ancestorClass, 'db', Config::UNINHERITED)
- );
- if($includerelations){
- $types['has_one'] = (array)singleton($ancestorClass)->uninherited('has_one', true);
- $types['has_many'] = (array)singleton($ancestorClass)->uninherited('has_many', true);
- $types['many_many'] = (array)singleton($ancestorClass)->uninherited('many_many', true);
- }
- foreach($types as $type => $attrs) {
- foreach($attrs as $name => $spec)
- $autoLabels[$name] = _t("{$ancestorClass}.{$type}_{$name}",FormField::name_to_label($name));
- }
- }
-
- $labels = array_merge((array)$autoLabels, (array)$customLabels);
+ $cacheKey = $this->class . '_' . $includerelations;
- $this->extend('updateFieldLabels', $labels);
+ if(!isset(self::$_cache_field_labels[$cacheKey])) {
+ $customLabels = $this->stat('field_labels');
+ $autoLabels = array();
+
+ // get all translated static properties as defined in i18nCollectStatics()
+ $ancestry = ClassInfo::ancestry($this->class);
+ $ancestry = array_reverse($ancestry);
+ if($ancestry) foreach($ancestry as $ancestorClass) {
+ if($ancestorClass == 'ViewableData') break;
+ $types = array(
+ 'db' => (array)Config::inst()->get($ancestorClass, 'db', Config::UNINHERITED)
+ );
+ if($includerelations){
+ $types['has_one'] = (array)singleton($ancestorClass)->uninherited('has_one', true);
+ $types['has_many'] = (array)singleton($ancestorClass)->uninherited('has_many', true);
+ $types['many_many'] = (array)singleton($ancestorClass)->uninherited('many_many', true);
+ }
+ foreach($types as $type => $attrs) {
+ foreach($attrs as $name => $spec) {
+ // var_dump("{$ancestorClass}.{$type}_{$name}");
+ $autoLabels[$name] = _t("{$ancestorClass}.{$type}_{$name}",FormField::name_to_label($name));
+ }
+ }
+ }
- return $labels;
+ $labels = array_merge((array)$autoLabels, (array)$customLabels);
+ $this->extend('updateFieldLabels', $labels);
+ self::$_cache_field_labels[$cacheKey] = $labels;
+ }
+
+ return self::$_cache_field_labels[$cacheKey];
}
/**

0 comments on commit b81863c

Please sign in to comment.