|
|
@@ -1532,45 +1532,59 @@ class Model extends Object {
|
|
|
$keys['old'] = isset($keys['old']) ? $keys['old'] : array();
|
|
|
|
|
|
foreach ($this->belongsTo as $parent => $assoc) {
|
|
|
- $foreignKey = $assoc['foreignKey'];
|
|
|
- $fkQuoted = $this->escapeField($assoc['foreignKey']);
|
|
|
-
|
|
|
if (!empty($assoc['counterCache'])) {
|
|
|
- if ($assoc['counterCache'] === true) {
|
|
|
- $assoc['counterCache'] = Inflector::underscore($this->alias) . '_count';
|
|
|
- }
|
|
|
- if (!$this->{$parent}->hasField($assoc['counterCache'])) {
|
|
|
- continue;
|
|
|
+ if (!is_array($assoc['counterCache'])) {
|
|
|
+ if (isset($assoc['counterScope'])) {
|
|
|
+ $assoc['counterCache'] = array($assoc['counterCache'] => $assoc['counterScope']);
|
|
|
+ } else {
|
|
|
+ $assoc['counterCache'] = array($assoc['counterCache'] => array());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- if (!array_key_exists($foreignKey, $keys)) {
|
|
|
- $keys[$foreignKey] = $this->field($foreignKey);
|
|
|
- }
|
|
|
- $recursive = (isset($assoc['counterScope']) ? 0 : -1);
|
|
|
- $conditions = ($recursive === 0) ? (array)$assoc['counterScope'] : array();
|
|
|
-
|
|
|
- if (isset($keys['old'][$foreignKey])) {
|
|
|
- if ($keys['old'][$foreignKey] != $keys[$foreignKey]) {
|
|
|
- $conditions[$fkQuoted] = $keys['old'][$foreignKey];
|
|
|
- $count = intval($this->find('count', compact('conditions', 'recursive')));
|
|
|
-
|
|
|
- $this->{$parent}->updateAll(
|
|
|
- array($assoc['counterCache'] => $count),
|
|
|
- array($this->{$parent}->escapeField() => $keys['old'][$foreignKey])
|
|
|
- );
|
|
|
+ $foreignKey = $assoc['foreignKey'];
|
|
|
+ $fkQuoted = $this->escapeField($assoc['foreignKey']);
|
|
|
+
|
|
|
+ foreach ($assoc['counterCache'] as $field => $conditions) {
|
|
|
+ if (!is_string($field)) {
|
|
|
+ $field = Inflector::underscore($this->alias) . '_count';
|
|
|
+ }
|
|
|
+ if (!$this->{$parent}->hasField($field)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if ($conditions === true) {
|
|
|
+ $conditions = array();
|
|
|
+ } else {
|
|
|
+ $conditions = (array)$conditions;
|
|
|
}
|
|
|
- }
|
|
|
- $conditions[$fkQuoted] = $keys[$foreignKey];
|
|
|
|
|
|
- if ($recursive === 0) {
|
|
|
- $conditions = array_merge($conditions, (array)$assoc['counterScope']);
|
|
|
- }
|
|
|
- $count = intval($this->find('count', compact('conditions', 'recursive')));
|
|
|
+ if (!array_key_exists($foreignKey, $keys)) {
|
|
|
+ $keys[$foreignKey] = $this->field($foreignKey);
|
|
|
+ }
|
|
|
+ $recursive = (empty($conditions) ? -1 : 0);
|
|
|
|
|
|
- $this->{$parent}->updateAll(
|
|
|
- array($assoc['counterCache'] => $count),
|
|
|
- array($this->{$parent}->escapeField() => $keys[$foreignKey])
|
|
|
- );
|
|
|
+ if (isset($keys['old'][$foreignKey])) {
|
|
|
+ if ($keys['old'][$foreignKey] != $keys[$foreignKey]) {
|
|
|
+ $conditions[$fkQuoted] = $keys['old'][$foreignKey];
|
|
|
+ $count = intval($this->find('count', compact('conditions', 'recursive')));
|
|
|
+
|
|
|
+ $this->{$parent}->updateAll(
|
|
|
+ array($field => $count),
|
|
|
+ array($this->{$parent}->escapeField() => $keys['old'][$foreignKey])
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $conditions[$fkQuoted] = $keys[$foreignKey];
|
|
|
+
|
|
|
+ if ($recursive === 0) {
|
|
|
+ $conditions = array_merge($conditions, (array)$conditions);
|
|
|
+ }
|
|
|
+ $count = intval($this->find('count', compact('conditions', 'recursive')));
|
|
|
+
|
|
|
+ $this->{$parent}->updateAll(
|
|
|
+ array($field => $count),
|
|
|
+ array($this->{$parent}->escapeField() => $keys[$foreignKey])
|
|
|
+ );
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|