1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class CounterCacheBehavior extends ModelBehavior {
 
    var $defaultSettings = array('method' => 'count');
 
    function setup(&$model, $config = array()) {
        // not yet
        $method = $this->defaultSettings['method'];
 
        if (!empty($config['method'])) {
            $method = $config['method'];
        }
 
        $this->settings[$model->name] = array(
            'method' => $method
        );
    }
 
    function afterSave(&$model) {
        foreach ($model->belongsTo as $parent => $assoc) {
            if (!isset($this->{$parent})) {
                $parent = $assoc['className'];
            }
            if (!empty($assoc['counterCache'])) {
                $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;
                    $model->{$parent}->saveField($assoc['counterCache'], intval($model->findCount("{$model->name}.{$assoc['foreignKey']} = {$foreign_id} AND {$conditions}", -1)));
                }
            }
        }
        return true;
    }
 
    function beforeDelete(&$model) {
        foreach ($model->belongsTo as $parent => $assoc) {
            if (!isset($this->{$parent})) {
                $parent = $assoc['className'];
            }
            if (!empty($assoc['counterCache'])) {
                $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'])) {
                    $associations = $model->find(array($model->primaryKey => $model->id), array("{$model->name}.{$assoc['foreignKey']}"), null, -1);
                    $this->assocs[$assoc['foreignKey']] = $associations[$model->name][$assoc['foreignKey']];
                }
            }
        }
        return true;
    }
 
    function afterDelete(&$model) {
        foreach ($model->belongsTo as $parent => $assoc) {
            if (!isset($this->{$parent})) {
                $parent = $assoc['className'];
            }
            if (!empty($assoc['counterCache'])) {
                $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']];
                $model->{$parent}->saveField($assoc['counterCache'], $count);
            }
        }
        return true;
    }
 
} // end class