01.23
php
saved
kain
Note
Implementing counterCache support in cake 1.2.
not tested very much but should work.
Implementing counterCache support in cake 1.2.
not tested very much but should work.
- <?php
- /* SVN FILE: $Id$ */
- /**
- * CounterCache Behavior for CakePHP 1.2
- *
- * @filesource
- * @copyright Copyright (c) 2006, iCoreTech
- * @author $Author$
- * @version $Rev$
- * @modifiedby $LastChangedBy$
- * @lastmodified $Date$
- * @license http://www.opensource.org/licenses/mit-license.php The MIT License
- */
- /**
- Usage example:
- var $actsAs = array(
- 'CounterCache' => array('method' => 'count', 'updatetimefields' => true)
- );
- TODO: move $this->assocs[$assoc['foreignKey']] to $this->settings[$model->name]
- TODO: another method, increment/decrement instead of count
- */
- class CounterCacheBehavior extends ModelBehavior {
- $method = $this->defaultSettings['method'];
- $updatetime = $this->defaultSettings['updatetimefields'];
- $method = $config['method'];
- }
- if ($config['updatetimefields'] === false || $config['updatetimefields'] === true) {
- $updatetime = $config['updatetimefields'];
- } else {
- $updatetime = true;
- }
- if ($method != 'count') {
- // only count for now
- $method = 'count';
- }
- 'method' => $method,
- 'updatetimefields' => $updatetime
- );
- }
- function afterSave(&$model) {
- foreach ($model->__associations as $association) {
- $model->__backAssociation[$association] = $model->{$association};
- }
- }
- foreach ($model->__backAssociation['belongsTo'] as $parent => $assoc) {
- $parent = $assoc['className'];
- }
- $assoc['counterCache'] = $assoc['counterCache'] === true ? Inflector::underscore($model->name).'_count' : $assoc['counterCache'];
- $assoc['foreignKey'] = !empty($assoc['foreignKey']) ? $assoc['foreignKey'] : Inflector::singularize($parent).'_id';
- if ($model->{$parent}->hasField($assoc['counterCache']) && !empty($model->data[$model->name][$assoc['foreignKey']])) {
- $foreign_id = $model->data[$model->name][$assoc['foreignKey']];
- $conditions = !empty($assoc['counterCacheConditions']) ? $assoc['counterCacheConditions'] : '1 = 1';
- $model->{$parent}->create();
- $model->{$parent}->id = $foreign_id;
- $count = intval($model->findCount("{$model->name}.{$assoc['foreignKey']} = {$foreign_id} AND {$conditions}", -1));
- if ($this->settings[$model->name]['updatetimefields'] === true) {
- $model->{$parent}->saveField($assoc['counterCache'], $count);
- } else {
- $model->data[$parent][$assoc['counterCache']] = $count;
- }
- }
- }
- } // end foreach
- return true;
- }
- function beforeDelete(&$model) {
- foreach ($model->__associations as $association) {
- $model->__backAssociation[$association] = $model->{$association};
- }
- }
- foreach ($model->__backAssociation['belongsTo'] as $parent => $assoc) {
- $parent = $assoc['className'];
- }
- $assoc['counterCache'] = $assoc['counterCache'] === true ? Inflector::underscore($model->name).'_count' : $assoc['counterCache'];
- $assoc['foreignKey'] = !empty($assoc['foreignKey']) ? $assoc['foreignKey'] : Inflector::singularize($parent).'_id';
- if ($model->{$parent}->hasField($assoc['counterCache'])) {
- $this->assocs[$assoc['foreignKey']] = $associations[$model->name][$assoc['foreignKey']];
- }
- }
- }
- return true;
- }
- function afterDelete(&$model) {
- foreach ($model->__associations as $association) {
- $model->__backAssociation[$association] = $model->{$association};
- }
- }
- foreach ($model->__backAssociation['belongsTo'] as $parent => $assoc) {
- $parent = $assoc['className'];
- }
- $assoc['counterCache'] = $assoc['counterCache'] === true ? Inflector::underscore($model->name).'_count' : $assoc['counterCache'];
- $assoc['foreignKey'] = !empty($assoc['foreignKey']) ? $assoc['foreignKey'] : Inflector::singularize($parent).'_id';
- $conditions = !empty($assoc['counterCacheConditions']) ? $assoc['counterCacheConditions'] : '1 = 1';
- $count = intval($model->findCount("{$model->name}.{$assoc['foreignKey']} = {$this->assocs[$assoc['foreignKey']]} AND {$conditions}", -1));
- $model->{$parent}->id = $this->assocs[$assoc['foreignKey']];
- if ($this->settings[$model->name]['updatetimefields'] === true) {
- $model->{$parent}->saveField($assoc['counterCache'], $count);
- } else {
- $model->data[$parent][$assoc['counterCache']] = $count;
- }
- }
- }
- return true;
- }
- } // end class
Parsed in 0.311 seconds, using GeSHi 1.0.7.14