Fixing issue where non-reset associations would get reset by resetAssociations if __backAssociations existed. Test cases from 'real34' added. Fixes #868

This commit is contained in:
mark_story 2010-07-01 12:39:50 -04:00
parent 8581350d24
commit 17a7a96ba2
2 changed files with 43 additions and 5 deletions

View file

@ -532,7 +532,6 @@ class Model extends Overloadable {
if ($reset === true) {
$this->__backAssociation[$assoc] = $this->{$assoc};
}
foreach ($model as $key => $value) {
$assocName = $key;
@ -542,6 +541,10 @@ class Model extends Overloadable {
}
$modelName = $assocName;
$this->{$assoc}[$assocName] = $value;
if ($reset === false && isset($this->__backAssociation[$assoc])) {
$this->__backAssociation[$assoc][$assocName] = $value;
}
}
}
$this->__createLinks();
@ -575,8 +578,8 @@ class Model extends Overloadable {
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 @@ class Model extends Overloadable {
}
/**
* 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

View file

@ -4720,6 +4720,41 @@ class ModelReadTest extends BaseModelTest {
$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
*