Skip to content

Commit

Permalink
Fixing issues where multiple reset bindModel() calls would cause inco…
Browse files Browse the repository at this point in the history
…rrect associations to be restored.

Also fixing issues where multiple calls to unbindModel() would cause incorrect associations to be restored.
Tests added.
Fixes #889
  • Loading branch information
markstory committed Jul 3, 2010
1 parent 7e4c3f9 commit 8edf207
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
10 changes: 5 additions & 5 deletions cake/libs/model/model.php
Expand Up @@ -538,7 +538,7 @@ function bind($model, $options = array(), $permanent = true) {
*/
function bindModel($params, $reset = true) {
foreach ($params as $assoc => $model) {
if ($reset === true) {
if ($reset === true && !isset($this->__backAssociation[$assoc])) {
$this->__backAssociation[$assoc] = $this->{$assoc};
}
foreach ($model as $key => $value) {
Expand Down Expand Up @@ -579,13 +579,13 @@ function bindModel($params, $reset = true) {
*/
function unbindModel($params, $reset = true) {
foreach ($params as $assoc => $models) {
if ($reset === true) {
if ($reset === true && !isset($this->__backAssociation[$assoc])) {
$this->__backAssociation[$assoc] = $this->{$assoc};
}

foreach ($models as $model) {
$this->__backAssociation = array_merge($this->__backAssociation, $this->{$assoc});
unset($this->__backAssociation[$model]);
if ($reset === false && isset($this->__backAssociation[$assoc][$model])) {
unset($this->__backAssociation[$assoc][$model]);
}
unset($this->{$assoc}[$model]);
}
}
Expand Down
68 changes: 68 additions & 0 deletions cake/tests/cases/libs/model/model_read.test.php
Expand Up @@ -4761,6 +4761,22 @@ function testBindMultipleTimes() {
$this->assertEqual($result, $expected);
}

/**
* test that multiple reset = true calls to bindModel() result in the original associations.
*
* @return void
*/
function testBindModelMultipleTimesResetCorrectly() {
$this->loadFixtures('User', 'Comment', 'Article');
$TestModel =& new User();

$TestModel->bindModel(array('hasMany' => array('Comment')));
$TestModel->bindModel(array('hasMany' => array('Comment')));
$TestModel->resetAssociations();

$this->assertFalse(isset($TestModel->hasMany['Comment']), 'Association left behind');
}

/**
* testBindMultipleTimes method with different reset settings
*
Expand Down Expand Up @@ -4815,6 +4831,58 @@ function bindWithCustomPrimaryKey() {
$this->assertFalse(empty($result));
}

/**
* test that calling unbindModel() with reset == true multiple times
* leaves associations in the correct state.
*
* @return void
*/
function testUnbindMultipleTimesResetCorrectly() {
$this->loadFixtures('User', 'Comment', 'Article');
$TestModel =& new Article10();

$TestModel->unbindModel(array('hasMany' => array('Comment')));
$TestModel->unbindModel(array('hasMany' => array('Comment')));
$TestModel->resetAssociations();

$this->assertTrue(isset($TestModel->hasMany['Comment']), 'Association permanently removed');
}

/**
* testBindMultipleTimes method with different reset settings
*
* @access public
* @return void
*/
function testUnBindMultipleTimesWithDifferentResetSettings() {
$this->loadFixtures('User', 'Comment', 'Article');
$TestModel =& new Comment();

$result = array_keys($TestModel->belongsTo);
$expected = array('Article', 'User');
$this->assertEqual($result, $expected);

$result = $TestModel->unbindModel(array(
'belongsTo' => array('User')
));
$this->assertTrue($result);
$result = $TestModel->unbindModel(
array('belongsTo' => array('Article')),
false
);
$this->assertTrue($result);

$result = array_keys($TestModel->belongsTo);
$expected = array();
$this->assertEqual($result, $expected);

$TestModel->resetAssociations();

$result = array_keys($TestModel->belongsTo);
$expected = array('User');
$this->assertEqual($result, $expected);
}

/**
* testAssociationAfterFind method
*
Expand Down

0 comments on commit 8edf207

Please sign in to comment.