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...
1 parent 7e4c3f9 commit 8edf207ffe9e254307e14863ef51e58050afe344 @markstory markstory committed Jul 3, 2010
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
View
@@ -538,7 +538,7 @@ function bind($model, $options = array(), $permanent = true) {
*/
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) {
@@ -579,13 +579,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]);
}
}
@@ -4762,6 +4762,22 @@ function testBindMultipleTimes() {
}
/**
+ * 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
*
* @access public
@@ -4816,6 +4832,58 @@ function bindWithCustomPrimaryKey() {
}
/**
+ * 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
*
* @access public

0 comments on commit 8edf207

Please sign in to comment.