diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index d1da6549620..05b655975b3 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -1420,7 +1420,7 @@ function _prepareUpdateFields($data) { return array(); } $old = $this->find('first', array( - 'conditions' => array('id' => $this->id), + 'conditions' => array($this->primaryKey => $this->id), 'fields' => array_values($included), 'recursive' => -1 )); diff --git a/cake/tests/cases/libs/model/model.test.php b/cake/tests/cases/libs/model/model.test.php index 2705657019e..42170ae0c6c 100644 --- a/cake/tests/cases/libs/model/model.test.php +++ b/cake/tests/cases/libs/model/model.test.php @@ -63,9 +63,11 @@ class ModelTest extends CakeTestCase { 'core.dependency', 'core.story', 'core.stories_tag', 'core.cd', 'core.book', 'core.basket', 'core.overall_favorite', 'core.account', 'core.content', 'core.content_account', 'core.film_file', 'core.test_plugin_article', 'core.test_plugin_comment', 'core.uuiditem', - 'core.counter_cache_user', 'core.counter_cache_post', 'core.uuidportfolio', - 'core.uuiditems_uuidportfolio', 'core.uuiditems_uuidportfolio_numericid', - 'core.fruit', 'core.fruits_uuid_tag', 'core.uuid_tag' + 'core.counter_cache_user', 'core.counter_cache_post', + 'core.counter_cache_user_nonstandard_primary_key', + 'core.counter_cache_post_nonstandard_primary_key', 'core.uuidportfolio', + 'core.uuiditems_uuidportfolio', 'core.uuiditems_uuidportfolio_numericid', 'core.fruit', + 'core.fruits_uuid_tag', 'core.uuid_tag' ); /** * start method @@ -3876,7 +3878,6 @@ function testCounterCacheIncrease() { $result = $user[$User->alias]['post_count']; $expected = 3; $this->assertEqual($result, $expected); - } /** * Tests that counter caches are updated when records are deleted @@ -3919,6 +3920,32 @@ function testCounterCacheUpdated() { $this->assertEqual($users[0]['User']['post_count'], 1); $this->assertEqual($users[1]['User']['post_count'], 2); } +/** + * Test counter cache with models that use a non-standard (i.e. not using 'id') + * as their primary key. + * + * @access public + * @return void + */ + function testCounterCacheWithNonstandardPrimaryKey() { + $this->loadFixtures( + 'CounterCacheUserNonstandardPrimaryKey', + 'CounterCachePostNonstandardPrimaryKey' + ); + + $User = new CounterCacheUserNonstandardPrimaryKey(); + $Post = new CounterCachePostNonstandardPrimaryKey(); + + $data = $Post->find('first', array( + 'conditions' => array('pid' => 1),'recursive' => -1 + )); + $data[$Post->alias]['uid'] = 301; + $Post->save($data); + + $users = $User->find('all',array('order' => 'User.uid')); + $this->assertEqual($users[0]['User']['post_count'], 1); + $this->assertEqual($users[1]['User']['post_count'], 2); + } /** * test Counter Cache With Self Joining table * diff --git a/cake/tests/cases/libs/model/models.php b/cake/tests/cases/libs/model/models.php index 839e9f6abaf..617491c9072 100644 --- a/cake/tests/cases/libs/model/models.php +++ b/cake/tests/cases/libs/model/models.php @@ -2858,7 +2858,6 @@ class TranslatedArticle extends CakeTestModel { class CounterCacheUser extends CakeTestModel { var $name = 'CounterCacheUser'; var $alias = 'User'; - var $fixture = 'counter_cache_user'; var $hasMany = array('Post' => array( 'className' => 'CounterCachePost', @@ -2869,7 +2868,6 @@ class CounterCacheUser extends CakeTestModel { class CounterCachePost extends CakeTestModel { var $name = 'CounterCachePost'; var $alias = 'Post'; - var $fixture = 'counter_cache_user'; var $belongsTo = array('User' => array( 'className' => 'CounterCacheUser', @@ -2878,6 +2876,29 @@ class CounterCachePost extends CakeTestModel { )); } +class CounterCacheUserNonstandardPrimaryKey extends CakeTestModel { + var $name = 'CounterCacheUserNonstandardPrimaryKey'; + var $alias = 'User'; + var $primaryKey = 'uid'; + + var $hasMany = array('Post' => array( + 'className' => 'CounterCachePostNonstandardPrimaryKey', + 'foreignKey' => 'uid' + )); +} + +class CounterCachePostNonstandardPrimaryKey extends CakeTestModel { + var $name = 'CounterCachePostNonstandardPrimaryKey'; + var $alias = 'Post'; + var $primaryKey = 'pid'; + + var $belongsTo = array('User' => array( + 'className' => 'CounterCacheUserNonstandardPrimaryKey', + 'foreignKey' => 'uid', + 'counterCache' => true + )); +} + class ArticleB extends CakeTestModel { var $name = 'ArticleB'; var $useTable = 'articles';