Permalink
Browse files

Adding condition for counterCache update keys. Fixes issues where cou…

…nter cache wouldn't be updated. Fixes #450
  • Loading branch information...
1 parent e304c48 commit 028668b7a63f464caff5b851dd489c5c0bc14fcc @markstory markstory committed Mar 13, 2010
Showing with 21 additions and 1 deletion.
  1. +4 −1 cake/libs/model/model.php
  2. +17 −0 cake/tests/cases/libs/model/model_delete.test.php
View
5 cake/libs/model/model.php
@@ -1801,7 +1801,10 @@ function delete($id = null, $cascade = true) {
$this->id = $id;
if (!empty($this->belongsTo)) {
- $keys = $this->find('first', array('fields' => $this->__collectForeignKeys()));
+ $keys = $this->find('first', array(
+ 'fields' => $this->__collectForeignKeys(),
+ 'conditions' => array($this->alias . '.' . $this->primaryKey => $id)
+ ));
}
if ($db->delete($this)) {
View
17 cake/tests/cases/libs/model/model_delete.test.php
@@ -256,6 +256,23 @@ function testDelete() {
}
/**
+ * test that delete() updates the correct records counterCache() records.
+ *
+ * @return void
+ */
+ function testDeleteUpdatingCounterCacheCorrectly() {
+ $this->loadFixtures('CounterCacheUser', 'CounterCachePost');
+ $User =& new CounterCacheUser();
+
+ $User->Post->delete(3);
+ $result = $User->read(null, 301);
+ $this->assertEqual($result['User']['post_count'], 0);
+
+ $result = $User->read(null, 66);
+ $this->assertEqual($result['User']['post_count'], 2);
+ }
+
+/**
* testDeleteAll method
*
* @access public

0 comments on commit 028668b

Please sign in to comment.