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) { if ($reset === true) {
$this->__backAssociation[$assoc] = $this->{$assoc}; $this->__backAssociation[$assoc] = $this->{$assoc};
} }
foreach ($model as $key => $value) { foreach ($model as $key => $value) {
$assocName = $key; $assocName = $key;
@ -542,6 +541,10 @@ class Model extends Overloadable {
} }
$modelName = $assocName; $modelName = $assocName;
$this->{$assoc}[$assocName] = $value; $this->{$assoc}[$assocName] = $value;
if ($reset === false && isset($this->__backAssociation[$assoc])) {
$this->__backAssociation[$assoc][$assocName] = $value;
}
} }
} }
$this->__createLinks(); $this->__createLinks();
@ -2341,9 +2344,9 @@ class Model extends Overloadable {
} }
/** /**
* Called only when bindTo<ModelName>() is used.
* This resets the association arrays for the model back * 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 * @return boolean Success
* @access public * @access public

View file

@ -4720,6 +4720,41 @@ class ModelReadTest extends BaseModelTest {
$this->assertEqual($result, $expected); $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 * test that bindModel behaves with Custom primary Key associations
* *