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 8581350 commit 17a7a96ba2768b7ea0bc44fd06ef13048609ae29
Showing with 43 additions and 5 deletions.
  1. +8 −5 cake/libs/model/model.php
  2. +35 −0 cake/tests/cases/libs/model/model_read.test.php
@@ -532,7 +532,6 @@ function bindModel($params, $reset = true) {
if ($reset === true) {
$this->__backAssociation[$assoc] = $this->{$assoc};
}
foreach ($model as $key => $value) {
$assocName = $key;
@@ -542,6 +541,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();
@@ -575,8 +578,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;
@@ -2341,9 +2344,9 @@ function __filterResults($results, $primary = true) {
}
/**
* 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
@@ -4720,6 +4720,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
*

0 comments on commit 17a7a96

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