Skip to content
Permalink
Browse files

Fixing issue where non-reset associations would get reset by resetAss…

…ociations if __backAssociations existed. Test cases from 'real34' added. Fixes #868
  • Loading branch information...
markstory committed Jul 1, 2010
1 parent 93beb2b commit 7e4c3f9244b1664f0fa3263e7b9e69af388bd82a
Showing with 45 additions and 7 deletions.
  1. +9 −6 cake/libs/model/model.php
  2. +36 −1 cake/tests/cases/libs/model/model_read.test.php
@@ -541,7 +541,6 @@ function bindModel($params, $reset = true) {
if ($reset === true) {
$this->__backAssociation[$assoc] = $this->{$assoc};
}
foreach ($model as $key => $value) {
$assocName = $key;
@@ -551,6 +550,10 @@ function bindModel($params, $reset = true) {
}
$modelName = $assocName;
$this->{$assoc}[$assocName] = $value;
if ($reset === false && isset($this->__backAssociation[$assoc])) {
$this->__backAssociation[$assoc][$assocName] = $value;
}
}
}
$this->__createLinks();
@@ -582,8 +585,8 @@ function unbindModel($params, $reset = true) {
foreach ($models as $model) {
$this->__backAssociation = array_merge($this->__backAssociation, $this->{$assoc});
unset ($this->__backAssociation[$model]);
unset ($this->{$assoc}[$model]);
unset($this->__backAssociation[$model]);
unset($this->{$assoc}[$model]);
}
}
return true;
@@ -2283,9 +2286,9 @@ function __filterResults($results, $primary = true) {
return $this->afterFind($results, $primary);
}
/**
* Called only when bindTo<ModelName>() is used.
* This resets the association arrays for the model back
* to those originally defined in the model.
* to those originally defined in the model. Normally called at the end
* of each call to Model::find()
*
* @return boolean Success
* @access public
@@ -2984,4 +2987,4 @@ function findNeighbours($conditions = null, $field, $value) {
if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
Overloadable::overload('Model');
}
?>
?>
@@ -4761,6 +4761,41 @@ function testBindMultipleTimes() {
$this->assertEqual($result, $expected);
}
/**
* testBindMultipleTimes method with different reset settings
*
* @access public
* @return void
*/
function testBindMultipleTimesWithDifferentResetSettings() {
$this->loadFixtures('User', 'Comment', 'Article');
$TestModel =& new User();
$result = $TestModel->hasMany;
$expected = array();
$this->assertEqual($result, $expected);
$result = $TestModel->bindModel(array(
'hasMany' => array('Comment')
));
$this->assertTrue($result);
$result = $TestModel->bindModel(
array('hasMany' => array('Article')),
false
);
$this->assertTrue($result);
$result = array_keys($TestModel->hasMany);
$expected = array('Comment', 'Article');
$this->assertEqual($result, $expected);
$TestModel->resetAssociations();
$result = array_keys($TestModel->hasMany);
$expected = array('Article');
$this->assertEqual($result, $expected);
}
/**
* test that bindModel behaves with Custom primary Key associations
*
@@ -7161,4 +7196,4 @@ function testRecursiveFindAllWithLimit() {
$this->assertEqual($result, $expected);
}
}
?>
?>

0 comments on commit 7e4c3f9

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