Permalink
Browse files

Removing hard-coded 'id' field from Model::_prepareUpdateFields. Fixes

…#6274.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8139 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information...
1 parent bf2829a commit 066629d5665e90c617359d8e789a77885bce9b9d @jperras jperras committed Apr 13, 2009
Showing with 55 additions and 7 deletions.
  1. +1 −1 cake/libs/model/model.php
  2. +31 −4 cake/tests/cases/libs/model/model.test.php
  3. +23 −2 cake/tests/cases/libs/model/models.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
));
@@ -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
@@ -3920,6 +3921,32 @@ function testCounterCacheUpdated() {
$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
*
* @return void
@@ -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';

0 comments on commit 066629d

Please sign in to comment.