Skip to content

Commit

Permalink
Removing hard-coded 'id' field from Model::_prepareUpdateFields. Fixes
Browse files Browse the repository at this point in the history
…#6274.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8139 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information
jperras committed Apr 13, 2009
1 parent bf2829a commit 066629d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cake/libs/model/model.php
Expand Up @@ -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
));
Expand Down
35 changes: 31 additions & 4 deletions cake/tests/cases/libs/model/model.test.php
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
*
Expand Down
25 changes: 23 additions & 2 deletions cake/tests/cases/libs/model/models.php
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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';
Expand Down

0 comments on commit 066629d

Please sign in to comment.