01.23 php saved
kain
Tags add more
cache and counter  
Note
Implementing counterCache support in cake 1.2.
not tested very much but should work.
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * CounterCache Behavior for CakePHP 1.2
  5. *
  6. * @filesource
  7. * @copyright      Copyright (c) 2006, iCoreTech
  8. * @author         $Author$
  9. * @version        $Rev$
  10. * @modifiedby     $LastChangedBy$
  11. * @lastmodified   $Date$
  12. * @license        http://www.opensource.org/licenses/mit-license.php The MIT License
  13. */
  14.  
  15. /**
  16.   Usage example:
  17.     var $actsAs = array(
  18.         'CounterCache' => array('method' => 'count', 'updatetimefields' => true)
  19.     );
  20.  
  21.     TODO: move $this->assocs[$assoc['foreignKey']] to $this->settings[$model->name]
  22.     TODO: another counter methodology
  23. */
  24.  
  25. class CounterCacheBehavior extends ModelBehavior {
  26.  
  27.     var $defaultSettings = array('method' => 'count', 'updatetimefields' => true);
  28.  
  29.     function setup(&$model, $config = array()) {
  30.         $method = $this->defaultSettings['method'];
  31.         $updatetime = $this->defaultSettings['updatetimefields'];
  32.  
  33.         if (!empty($config['method'])) {
  34.             $method = $config['method'];
  35.         }
  36.  
  37.         if ($config['updatetimefields'] === false || $config['updatetimefields'] === true) {
  38.             $updatetime = $config['updatetimefields'];
  39.         } else {
  40.             $updatetime = true;
  41.         }
  42.  
  43.         if ($method != 'count') {
  44.             // only count for now
  45.             $method = 'count';
  46.         }
  47.        
  48.         $this->settings[$model->name] = array(
  49.             'method' => $method,
  50.             'updatetimefields' => $updatetime
  51.         );
  52.     }
  53.  
  54.     function afterSave(&$model) {
  55.  
  56.         foreach ($model->__associations as $association) {
  57.             if (!empty($model->{$association})) {
  58.                 $model->__backAssociation[$association] = $model->{$association};
  59.             }
  60.         }
  61.         foreach ($model->__backAssociation['belongsTo'] as $parent => $assoc) {
  62.             if (!isset($model->{$parent})) {
  63.                 $parent = $assoc['className'];
  64.             }
  65.             if (!empty($assoc['counterCache'])) {
  66.                 $assoc['counterCache'] = $assoc['counterCache'] === true ? Inflector::underscore($model->name).'_count' : $assoc['counterCache'];
  67.                 $assoc['foreignKey'] = !empty($assoc['foreignKey']) ? $assoc['foreignKey'] : Inflector::singularize($parent).'_id';
  68.                 if ($model->{$parent}->hasField($assoc['counterCache']) && !empty($model->data[$model->name][$assoc['foreignKey']])) {
  69.                     $foreign_id = $model->data[$model->name][$assoc['foreignKey']];
  70.                     $conditions = !empty($assoc['counterCacheConditions']) ? $assoc['counterCacheConditions'] : '1 = 1';
  71.                     $model->{$parent}->create();
  72.                     $model->{$parent}->id = $foreign_id;
  73.                     $count = intval($model->findCount("{$model->name}.{$assoc['foreignKey']} = {$foreign_id} AND {$conditions}", -1));
  74.                     if ($this->settings[$model->name]['updatetimefields'] === true) {
  75.                         $model->{$parent}->saveField($assoc['counterCache'], $count);
  76.                     } else {
  77.                         $model->data[$parent][$assoc['counterCache']] = $count;
  78.                         $model->{$parent}->save($model->data[$parent], false, array($assoc['counterCache']));
  79.                     }
  80.                 }
  81.             }
  82.         } // end foreach
  83.  
  84.         return true;
  85.     }
  86.  
  87.     function beforeDelete(&$model) {
  88.         foreach ($model->__associations as $association) {
  89.             if (!empty($model->{$association})) {
  90.                 $model->__backAssociation[$association] = $model->{$association};
  91.             }
  92.         }
  93.         foreach ($model->__backAssociation['belongsTo'] as $parent => $assoc) {
  94.             if (!isset($model->{$parent})) {
  95.                 $parent = $assoc['className'];
  96.             }
  97.             if (!empty($assoc['counterCache'])) {
  98.                 $assoc['counterCache'] = $assoc['counterCache'] === true ? Inflector::underscore($model->name).'_count' : $assoc['counterCache'];
  99.                 $assoc['foreignKey'] = !empty($assoc['foreignKey']) ? $assoc['foreignKey'] : Inflector::singularize($parent).'_id';
  100.                 if ($model->{$parent}->hasField($assoc['counterCache'])) {
  101.                     $associations = $model->find(array($model->primaryKey => $model->id), array("{$model->name}.{$assoc['foreignKey']}"), null, -1);
  102.                     $this->assocs[$assoc['foreignKey']] = $associations[$model->name][$assoc['foreignKey']];
  103.                 }
  104.             }
  105.         }
  106.         return true;
  107.     }
  108.  
  109.     function afterDelete(&$model) {
  110.         foreach ($model->__associations as $association) {
  111.             if (!empty($model->{$association})) {
  112.                 $model->__backAssociation[$association] = $model->{$association};
  113.             }
  114.         }
  115.         foreach ($model->__backAssociation['belongsTo'] as $parent => $assoc) {
  116.             if (!isset($model->{$parent})) {
  117.                 $parent = $assoc['className'];
  118.             }
  119.             if (!empty($assoc['counterCache'])) {
  120.                 $assoc['counterCache'] = $assoc['counterCache'] === true ? Inflector::underscore($model->name).'_count' : $assoc['counterCache'];
  121.                 $assoc['foreignKey'] = !empty($assoc['foreignKey']) ? $assoc['foreignKey'] : Inflector::singularize($parent).'_id';
  122.                 $conditions = !empty($assoc['counterCacheConditions']) ? $assoc['counterCacheConditions'] : '1 = 1';
  123.                 $count = intval($model->findCount("{$model->name}.{$assoc['foreignKey']} = {$this->assocs[$assoc['foreignKey']]} AND {$conditions}", -1));
  124.                 $model->{$parent}->id = $this->assocs[$assoc['foreignKey']];
  125.                
  126.                 if ($this->settings[$model->name]['updatetimefields'] === true) {
  127.                     $model->{$parent}->saveField($assoc['counterCache'], $count);
  128.                 } else {
  129.                     $model->data[$parent][$assoc['counterCache']] = $count;
  130.                     $model->{$parent}->save($model->data[$parent], false, array($assoc['counterCache']));
  131.                 }
  132.  
  133.             }
  134.         }
  135.         return true;
  136.     }
  137.  
  138. } // end class
Parsed in 0.302 seconds, using GeSHi 1.0.7.14

Modify this Paste