Skip to content
Permalink
Browse files

Fixing issues where multiple reset bindModel() calls would cause inco…

…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 f9de88d commit 4cd43553b4da5163dabc0219000466e946117b41
Showing with 73 additions and 5 deletions.
  1. +5 −5 cake/libs/model/model.php
  2. +68 −0 cake/tests/cases/libs/model/model_read.test.php
@@ -529,7 +529,7 @@ function call__($method, $params) {
*/
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) {
@@ -572,13 +572,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]);
}
}
@@ -4720,6 +4720,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
*
@@ -4774,6 +4790,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
*

0 comments on commit 4cd4355

Please sign in to comment.
You can’t perform that action at this time.