mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 10:36:16 +00:00
Adding foreign key check to Model::updateCounterCache(), fixes #4049
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6425 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
6e537beb73
commit
881cf3fc0a
3 changed files with 40 additions and 17 deletions
|
@ -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'])) {
|
||||
|
|
|
@ -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();
|
||||
|
|
13
cake/tests/fixtures/item_fixture.php
vendored
13
cake/tests/fixtures/item_fixture.php
vendored
|
@ -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')
|
||||
);
|
||||
}
|
||||
?>
|
Loading…
Add table
Reference in a new issue