Skip to content

Commit

Permalink
Fixing counterCache behavior when updating an existing record's count…
Browse files Browse the repository at this point in the history
…erScope value. Thanks for the patch, mattcurry. Fixes #6270.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8155 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information
jperras committed Apr 25, 2009
1 parent 9ab6339 commit 8bb11e4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
17 changes: 8 additions & 9 deletions cake/libs/model/model.php
Expand Up @@ -1374,16 +1374,15 @@ function updateCounterCache($keys = array(), $created = false) {
$conditions = ($recursive == 1) ? (array)$assoc['counterScope'] : array();

if (isset($keys['old'][$foreignKey])) {
if ($keys['old'][$foreignKey] == $keys[$foreignKey]) {
continue;
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])
);
}
$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])
);
}
$conditions[$fkQuoted] = $keys[$foreignKey];

Expand Down
4 changes: 4 additions & 0 deletions cake/tests/cases/libs/model/model.test.php
Expand Up @@ -3990,6 +3990,10 @@ function testSaveWithCounterCacheScope() {
$TestModel2->saveField('published', true);
$result = $TestModel->findById(1);
$this->assertIdentical($result['Syfile']['item_count'], '2');

$TestModel2->save(array('id' => 1, 'syfile_id' => 1, 'published'=> false));
$result = $TestModel->findById(1);
$this->assertIdentical($result['Syfile']['item_count'], '1');
}
/**
* testDel method
Expand Down

0 comments on commit 8bb11e4

Please sign in to comment.