From 8bb11e49d7fb1fb708caba36b3ede5c35e6f424a Mon Sep 17 00:00:00 2001 From: jperras Date: Sat, 25 Apr 2009 01:17:21 +0000 Subject: [PATCH] Fixing counterCache behavior when updating an existing record's counterScope 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 --- cake/libs/model/model.php | 17 ++++++++--------- cake/tests/cases/libs/model/model.test.php | 4 ++++ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index 5238bb242..1becede48 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -1374,16 +1374,15 @@ class Model extends Overloadable { $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]; diff --git a/cake/tests/cases/libs/model/model.test.php b/cake/tests/cases/libs/model/model.test.php index 595335172..77149c1cc 100644 --- a/cake/tests/cases/libs/model/model.test.php +++ b/cake/tests/cases/libs/model/model.test.php @@ -3990,6 +3990,10 @@ class ModelTest extends CakeTestCase { $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