diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index ac12f4136..3ed461426 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -1379,6 +1379,9 @@ class Model extends Overloadable { if ($assoc['counterCache'] === true) { $assoc['counterCache'] = Inflector::underscore($this->alias) . '_count'; } + if (!isset($keys[$assoc['foreignKey']]) || empty($keys[$assoc['foreignKey']])) { + $keys[$assoc['foreignKey']] = $this->field($assoc['foreignKey']); + } if ($this->{$parent}->hasField($assoc['counterCache'])) { $conditions = array($this->escapeField($assoc['foreignKey']) => $keys[$assoc['foreignKey']]); if (isset($assoc['counterScope'])) { diff --git a/cake/tests/cases/libs/model/model.test.php b/cake/tests/cases/libs/model/model.test.php index 1f0f17f81..bee8c431e 100644 --- a/cake/tests/cases/libs/model/model.test.php +++ b/cake/tests/cases/libs/model/model.test.php @@ -539,17 +539,16 @@ class ModelTest extends CakeTestCase { $result = $this->Portfolio->find(array('id' => 2), null, null, 3); $expected = array('Portfolio' => array( - 'id' => 2, 'seller_id' => 1, 'name' => 'Portfolio 2'), - 'Item' => array( - array('id' => 2, 'syfile_id' => 2, 'name' => 'Item 2', - 'ItemsPortfolio' => array('id' => 2, 'item_id' => 2, 'portfolio_id' => 2), - 'Syfile' => array('id' => 2, 'image_id' => 2, 'name' => 'Syfile 2', 'item_count' => null, - 'Image' => array('id' => 2, 'name' => 'Image 2'))), - - array('id' => 6, 'syfile_id' => 6, 'name' => 'Item 6', - 'ItemsPortfolio' => array('id' => 6, 'item_id' => 6, 'portfolio_id' => 2), - 'Syfile' => array('id' => 6, 'image_id' => null, 'name' => 'Syfile 6', 'item_count' => null, - 'Image' => array())))); + 'id' => 2, 'seller_id' => 1, 'name' => 'Portfolio 2'), + 'Item' => array( + array('id' => 2, 'syfile_id' => 2, 'published' => 0, 'name' => 'Item 2', + 'ItemsPortfolio' => array('id' => 2, 'item_id' => 2, 'portfolio_id' => 2), + 'Syfile' => array('id' => 2, 'image_id' => 2, 'name' => 'Syfile 2', 'item_count' => null, + 'Image' => array('id' => 2, 'name' => 'Image 2'))), + array('id' => 6, 'syfile_id' => 6, 'published' => 0, 'name' => 'Item 6', + 'ItemsPortfolio' => array('id' => 6, 'item_id' => 6, 'portfolio_id' => 2), + 'Syfile' => array('id' => 6, 'image_id' => null, 'name' => 'Syfile 6', 'item_count' => null, + 'Image' => array())))); $this->assertEqual($result, $expected); unset($this->Portfolio); } @@ -2349,6 +2348,26 @@ class ModelTest extends CakeTestCase { $this->assertIdentical($result['Syfile']['item_count'], '1'); } + function testSaveWithCounterCacheScope() { + $this->loadFixtures('Syfile', 'Item'); + $this->model =& new Syfile(); + $this->model2 =& new Item(); + $this->model2->belongsTo['Syfile']['counterCache'] = true; + $this->model2->belongsTo['Syfile']['counterScope'] = 'published = 1'; + + $result = $this->model->findById(1); + $this->assertIdentical($result['Syfile']['item_count'], null); + + $this->model2->save(array('name' => 'Item 7', 'syfile_id' => 1, 'published'=> 1)); + $result = $this->model->findById(1); + $this->assertIdentical($result['Syfile']['item_count'], '1'); + + $this->model2->id = 1; + $this->model2->saveField('published', 1); + $result = $this->model->findById(1); + $this->assertIdentical($result['Syfile']['item_count'], '2'); + } + function testDel() { $this->loadFixtures('Article'); $this->model =& new Article(); diff --git a/cake/tests/fixtures/item_fixture.php b/cake/tests/fixtures/item_fixture.php index 5ba6a5628..2c94f7397 100644 --- a/cake/tests/fixtures/item_fixture.php +++ b/cake/tests/fixtures/item_fixture.php @@ -37,15 +37,16 @@ class ItemFixture extends CakeTestFixture { var $fields = array( 'id' => array('type' => 'integer', 'key' => 'primary'), 'syfile_id' => array('type' => 'integer', 'null' => false), + 'published' => array('type' => 'boolean', 'null' => false), 'name' => array('type' => 'string', 'null' => false) ); var $records = array( - array('syfile_id' => 1, 'name' => 'Item 1'), - array('syfile_id' => 2, 'name' => 'Item 2'), - array('syfile_id' => 3, 'name' => 'Item 3'), - array('syfile_id' => 4, 'name' => 'Item 4'), - array('syfile_id' => 5, 'name' => 'Item 5'), - array('syfile_id' => 6, 'name' => 'Item 6') + array('syfile_id' => 1, 'published' => 0, 'name' => 'Item 1'), + array('syfile_id' => 2, 'published' => 0, 'name' => 'Item 2'), + array('syfile_id' => 3, 'published' => 0, 'name' => 'Item 3'), + array('syfile_id' => 4, 'published' => 0, 'name' => 'Item 4'), + array('syfile_id' => 5, 'published' => 0, 'name' => 'Item 5'), + array('syfile_id' => 6, 'published' => 0, 'name' => 'Item 6') ); } ?> \ No newline at end of file