01.23 php saved
kain
Tags add more
behavior  
Note
Implementing counterCache support in cake 1.2.
not tested very much but should work.
  1. class CounterCacheBehavior extends ModelBehavior {
  2.  
  3.     var $defaultSettings = array('method' => 'count');
  4.  
  5.     function setup(&$model, $config = array()) {
  6.         // not yet
  7.         $method = $this->defaultSettings['method'];
  8.  
  9.         if (!empty($config['method'])) {
  10.             $method = $config['method'];
  11.         }
  12.  
  13.         $this->settings[$model->name] = array(
  14.             'method' => $method
  15.         );
  16.     }
  17.  
  18.     function afterSave(&$model) {
  19.         foreach ($model->belongsTo as $parent => $assoc) {
  20.             if (!isset($this->{$parent})) {
  21.                 $parent = $assoc['className'];
  22.             }
  23.             if (!empty($assoc['counterCache'])) {
  24.                 $assoc['counterCache'] = $assoc['counterCache'] === true ? Inflector::underscore($model->name).'_count' : $assoc['counterCache'];
  25.                 $assoc['foreignKey'] = !empty($assoc['foreignKey']) ? $assoc['foreignKey'] : Inflector::singularize($parent).'_id';
  26.                 if ($model->{$parent}->hasField($assoc['counterCache']) && !empty($model->data[$model->name][$assoc['foreignKey']])) {
  27.                     $foreign_id = $model->data[$model->name][$assoc['foreignKey']];
  28.                     $conditions = !empty($assoc['counterCacheConditions']) ? $assoc['counterCacheConditions'] : '1 = 1';
  29.                     $model->{$parent}->create();
  30.                     $model->{$parent}->id = $foreign_id;
  31.                     $model->{$parent}->saveField($assoc['counterCache'], intval($model->findCount("{$model->name}.{$assoc['foreignKey']} = {$foreign_id} AND {$conditions}", -1)));
  32.                 }
  33.             }
  34.         }
  35.         return true;
  36.     }
  37.  
  38.     function beforeDelete(&$model) {
  39.         foreach ($model->belongsTo as $parent => $assoc) {
  40.             if (!isset($this->{$parent})) {
  41.                 $parent = $assoc['className'];
  42.             }
  43.             if (!empty($assoc['counterCache'])) {
  44.                 $assoc['counterCache'] = $assoc['counterCache'] === true ? Inflector::underscore($model->name).'_count' : $assoc['counterCache'];
  45.                 $assoc['foreignKey'] = !empty($assoc['foreignKey']) ? $assoc['foreignKey'] : Inflector::singularize($parent).'_id';
  46.                 if ($model->{$parent}->hasField($assoc['counterCache'])) {
  47.                     $associations = $model->find(array($model->primaryKey => $model->id), array("{$model->name}.{$assoc['foreignKey']}"), null, -1);
  48.                     $this->assocs[$assoc['foreignKey']] = $associations[$model->name][$assoc['foreignKey']];
  49.                 }
  50.             }
  51.         }
  52.         return true;
  53.     }
  54.  
  55.     function afterDelete(&$model) {
  56.         foreach ($model->belongsTo as $parent => $assoc) {
  57.             if (!isset($this->{$parent})) {
  58.                 $parent = $assoc['className'];
  59.             }
  60.             if (!empty($assoc['counterCache'])) {
  61.                 $assoc['counterCache'] = $assoc['counterCache'] === true ? Inflector::underscore($model->name).'_count' : $assoc['counterCache'];
  62.                 $assoc['foreignKey'] = !empty($assoc['foreignKey']) ? $assoc['foreignKey'] : Inflector::singularize($parent).'_id';
  63.                 $conditions = !empty($assoc['counterCacheConditions']) ? $assoc['counterCacheConditions'] : '1 = 1';
  64.                 $count = intval($model->findCount("{$model->name}.{$assoc['foreignKey']} = {$this->assocs[$assoc['foreignKey']]} AND {$conditions}", -1));
  65.                 $model->{$parent}->id = $this->assocs[$assoc['foreignKey']];
  66.                 $model->{$parent}->saveField($assoc['counterCache'], $count);
  67.             }
  68.         }
  69.         return true;
  70.     }
  71.  
  72. } // end class
Parsed in 0.220 seconds, using GeSHi 1.0.7.14

Modify this Paste