/
CounterCacheBehavior.php
194 lines (180 loc) · 5.19 KB
/
CounterCacheBehavior.php
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Model\Behavior;
use Cake\Event\Event;
use Cake\ORM\Association;
use Cake\ORM\Behavior;
use Cake\ORM\Entity;
use Cake\ORM\Table;
/**
* CounterCache behavior
*
* Enables models to cache the amount of connections in a given relation.
*
* Examples with Post model belonging to User model
*
* Regular counter cache
* {{{
* [
* 'Users' => [
* 'post_count'
* ]
* ]
* }}}
*
* Counter cache with scope
* {{{
* [
* 'Users' => [
* 'posts_published' => [
* 'conditions' => [
* 'published' => true
* ]
* ]
* ]
* ]
* }}}
*
* Counter cache using custom find
* {{{
* [
* 'Users' => [
* 'posts_published' => [
* 'finder' => 'published' // Will be using findPublished()
* ]
* ]
* ]
* }}}
*
* Counter cache using lambda function returning the count
* This is equivalent to example #2
* {{{
* [
* 'Users' => [
* 'posts_published' => function (Event $event, Entity $entity, Table $table) {
* $query = $table->find('all')->where([
* 'published' => true,
* 'user_id' => $entity->get('user_id')
* ]);
* return $query->count();
* }
* ]
* ]
* }}}
*
*/
class CounterCacheBehavior extends Behavior {
/**
* Keeping a reference to the table in order to,
* be able to retrieve associations and fetch records for counting.
*
* @var \Cake\ORM\Table
*/
protected $_table;
/**
* Constructor
*
* @param \Cake\ORM\Table $table The table this behavior is attached to.
* @param array $config The config for this behavior.
*/
public function __construct(Table $table, array $config = []) {
parent::__construct($table, $config);
$this->_table = $table;
}
/**
* afterSave callback.
*
* Makes sure to update counter cache when a new record is created or updated.
*
* @param \Cake\Event\Event $event The afterSave event that was fired.
* @param \Cake\ORM\Entity $entity The entity that was saved.
* @return void
*/
public function afterSave(Event $event, Entity $entity) {
$this->_processAssociations($event, $entity);
}
/**
* afterDelete callback.
*
* Makes sure to update counter cache when a record is deleted.
*
* @param \Cake\Event\Event $event The afterDelete event that was fired.
* @param \Cake\ORM\Entity $entity The entity that was deleted.
* @return void
*/
public function afterDelete(Event $event, Entity $entity) {
$this->_processAssociations($event, $entity);
}
/**
* Iterate all associations and update counter caches.
*
* @param \Cake\Event\Event $event Event instance.
* @param \Cake\ORM\Entity $entity Entity.
* @return void
*/
protected function _processAssociations(Event $event, Entity $entity) {
foreach ($this->_config as $assoc => $settings) {
$assoc = $this->_table->association($assoc);
$this->_processAssociation($event, $entity, $assoc, $settings);
}
}
/**
* Updates counter cache for a single association
*
* @param \Cake\Event\Event $event Event instance.
* @param \Cake\ORM\Entity $entity Entity
* @param Association $assoc The association object
* @param array $settings The settings for for counter cache for this association
* @return void
*/
protected function _processAssociation(Event $event, Entity $entity, Association $assoc, array $settings) {
$foreignKeys = (array)$assoc->foreignKey();
$primaryKeys = (array)$assoc->target()->primaryKey();
$countConditions = $entity->extract($foreignKeys);
$updateConditions = array_combine($primaryKeys, $countConditions);
foreach ($settings as $field => $config) {
if (is_int($field)) {
$field = $config;
$config = [];
}
if (!is_string($config) && is_callable($config)) {
$count = $config($event, $entity, $this->_table);
} else {
$count = $this->_getCount($config, $countConditions);
}
$assoc->target()->updateAll([$field => $count], $updateConditions);
}
}
/**
* Fetches and returns the count for a single field in an association
*
* @param array $config The counter cache configuration for a single field
* @param array $conditions Additional conditions given to the query
* @return int The number of relations matching the given config and conditions
*/
protected function _getCount(array $config, array $conditions) {
$finder = 'all';
if (!empty($config['finder'])) {
$finder = $config['finder'];
unset($config['finder']);
}
if (!isset($config['conditions'])) {
$config['conditions'] = [];
}
$config['conditions'] = array_merge($conditions, $config['conditions']);
$query = $this->_table->find($finder, $config);
return $query->count();
}
}