Permalink
Browse files

Fixing cascading delete, when using foreignKey false and setting cond…

…ition on hasOne
  • Loading branch information...
ceeram committed Nov 30, 2011
1 parent fc38787 commit 6b1fe87b8bfa28672f391100a2ef3ce11bdb639d
View
@@ -1868,12 +1868,17 @@ function _deleteDependent($id, $cascade) {
foreach (array_merge($this->hasMany, $this->hasOne) as $assoc => $data) {
if ($data['dependent'] === true) {
$model =& $this->{$assoc};
$conditions = array($model->escapeField($data['foreignKey']) => $id);
if ($data['conditions']) {
$conditions = array_merge((array)$data['conditions'], $conditions);
$model = $this->{$assoc};
if ($data['foreignKey'] === false && $data['conditions'] && in_array($this->name, $model->getAssociated('belongsTo'))) {
$model->recursive = 0;
$conditions = array($this->escapeField(null, $this->name) => $id);
} else {
$model->recursive = -1;
$conditions = array($model->escapeField($data['foreignKey']) => $id);
if ($data['conditions']) {
$conditions = array_merge((array)$data['conditions'], $conditions);
}
}
$model->recursive = -1;
if (isset($data['exclusive']) && $data['exclusive']) {
$model->deleteAll($conditions);
@@ -65,7 +65,8 @@ class BaseModelTest extends CakeTestCase {
'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', 'core.product_update_all', 'core.group_update_all'
'core.fruits_uuid_tag', 'core.uuid_tag', 'core.product_update_all', 'core.group_update_all',
'core.bidding', 'core.bidding_message'
);
/**
@@ -534,6 +534,86 @@ function testDeleteLinks() {
$this->assertTrue($result, 'deleteAll returned false when all no records matched conditions. %s');
}
/**
* testDeleteDependent method
*
* @access public
* @return void
*/
function testDeleteDependent() {
$this->loadFixtures('Bidding', 'BiddingMessage');
$Bidding = new Bidding();
$result = $Bidding->find('all');
$expected = array(
array(
'Bidding' => array('id' => 1, 'bid' => 'One', 'name' => 'Bid 1'),
'BiddingMessage' => array('bidding' => 'One', 'name' => 'Message 1'),
),
array(
'Bidding' => array('id' => 2, 'bid' => 'Two', 'name' => 'Bid 2'),
'BiddingMessage' => array('bidding' => 'Two', 'name' => 'Message 2'),
),
array(
'Bidding' => array('id' => 3, 'bid' => 'Three', 'name' => 'Bid 3'),
'BiddingMessage' => array('bidding' => 'Three', 'name' => 'Message 3'),
),
array(
'Bidding' => array('id' => 4, 'bid' => 'Five', 'name' => 'Bid 5'),
'BiddingMessage' => array('bidding' => '', 'name' => ''),
),
);
$this->assertEqual($result, $expected);
$Bidding->delete(4, true);
$result = $Bidding->find('all');
$expected = array(
array(
'Bidding' => array('id' => 1, 'bid' => 'One', 'name' => 'Bid 1'),
'BiddingMessage' => array('bidding' => 'One', 'name' => 'Message 1'),
),
array(
'Bidding' => array('id' => 2, 'bid' => 'Two', 'name' => 'Bid 2'),
'BiddingMessage' => array('bidding' => 'Two', 'name' => 'Message 2'),
),
array(
'Bidding' => array('id' => 3, 'bid' => 'Three', 'name' => 'Bid 3'),
'BiddingMessage' => array('bidding' => 'Three', 'name' => 'Message 3'),
),
);
$this->assertEqual($result, $expected);
$Bidding->delete(2, true);
$result = $Bidding->find('all');
$expected = array(
array(
'Bidding' => array('id' => 1, 'bid' => 'One', 'name' => 'Bid 1'),
'BiddingMessage' => array('bidding' => 'One', 'name' => 'Message 1'),
),
array(
'Bidding' => array('id' => 3, 'bid' => 'Three', 'name' => 'Bid 3'),
'BiddingMessage' => array('bidding' => 'Three', 'name' => 'Message 3'),
),
);
$this->assertEqual($result, $expected);
$result = $Bidding->BiddingMessage->find('all', array('order' => array('BiddingMessage.name' => 'ASC')));
$expected = array(
array(
'BiddingMessage' => array('bidding' => 'One', 'name' => 'Message 1'),
'Bidding' => array('id' => 1, 'bid' => 'One', 'name' => 'Bid 1'),
),
array(
'BiddingMessage' => array('bidding' => 'Three', 'name' => 'Message 3'),
'Bidding' => array('id' => 3, 'bid' => 'Three', 'name' => 'Bid 3'),
),
array(
'BiddingMessage' => array('bidding' => 'Four', 'name' => 'Message 4'),
'Bidding' => array('id' => '', 'bid' => '', 'name' => ''),
),
);
$this->assertEqual($result, $expected);
}
/**
* test deleteLinks with Multiple habtm associations
*
@@ -1104,6 +1104,76 @@ class Bid extends CakeTestModel {
var $belongsTo = array('Message');
}
/**
* BiddingMessage class
*
* @package cake
* @subpackage cake.tests.cases.libs.model
*/
class BiddingMessage extends CakeTestModel {
/**
* name property
*
* @var string 'BiddingMessage'
* @access public
*/
var $name = 'BiddingMessage';
/**
* primaryKey property
*
* @var string 'bidding'
* @access public
*/
var $primaryKey = 'bidding';
/**
* belongsTo property
*
* @var array
* @access public
*/
var $belongsTo = array(
'Bidding' => array(
'foreignKey' => false,
'conditions' => array('BiddingMessage.bidding = Bidding.bid')
)
);
}
/**
* Bidding class
*
* @package cake
* @subpackage cake.tests.cases.libs.model
*/
class Bidding extends CakeTestModel {
/**
* name property
*
* @var string 'Bidding'
* @access public
*/
var $name = 'Bidding';
/**
* hasOne property
*
* @var array
* @access public
*/
var $hasOne = array(
'BiddingMessage' => array(
'foreignKey' => false,
'conditions' => array('BiddingMessage.bidding = Bidding.bid'),
'dependent' => true
)
);
}
/**
* NodeAfterFind class
*
@@ -0,0 +1,61 @@
<?php
/**
* Short description for file.
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
* @package cake
* @subpackage cake.tests.fixtures
* @since CakePHP(tm) v 1.2.0.4667
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
/**
* Short description for class.
*
* @package cake
* @subpackage cake.tests.fixtures
*/
class BiddingFixture extends CakeTestFixture {
/**
* name property
*
* @var string 'Bidding'
* @access public
*/
var $name = 'Bidding';
/**
* fields property
*
* @var array
* @access public
*/
var $fields = array(
'id' => array('type' => 'integer', 'key' => 'primary'),
'bid' => array('type' => 'string', 'null' => false),
'name' => array('type' => 'string', 'null' => false)
);
/**
* records property
*
* @var array
* @access public
*/
var $records = array(
array('bid' => 'One', 'name' => 'Bid 1'),
array('bid' => 'Two', 'name' => 'Bid 2'),
array('bid' => 'Three', 'name' => 'Bid 3'),
array('bid' => 'Five', 'name' => 'Bid 5')
);
}
@@ -0,0 +1,60 @@
<?php
/**
* Short description for file.
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
* @package cake
* @subpackage cake.tests.fixtures
* @since CakePHP(tm) v 1.2.0.4667
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
/**
* Short description for class.
*
* @package cake
* @subpackage cake.tests.fixtures
*/
class BiddingMessageFixture extends CakeTestFixture {
/**
* name property
*
* @var string 'BiddingMessage'
* @access public
*/
var $name = 'BiddingMessage';
/**
* fields property
*
* @var array
* @access public
*/
var $fields = array(
'bidding' => array('type' => 'string', 'null' => false, 'key' => 'primary'),
'name' => array('type' => 'string', 'null' => false)
);
/**
* records property
*
* @var array
* @access public
*/
var $records = array(
array('bidding' => 'One', 'name' => 'Message 1'),
array('bidding' => 'Two', 'name' => 'Message 2'),
array('bidding' => 'Three', 'name' => 'Message 3'),
array('bidding' => 'Four', 'name' => 'Message 4')
);
}

0 comments on commit 6b1fe87

Please sign in to comment.