diff --git a/cake/libs/model/behaviors/containable.php b/cake/libs/model/behaviors/containable.php index 30fe1e7dc..be0890940 100644 --- a/cake/libs/model/behaviors/containable.php +++ b/cake/libs/model/behaviors/containable.php @@ -130,7 +130,11 @@ class ContainableBehavior extends ModelBehavior { if ($contain) { $backupBindings = array(); foreach ($this->types as $relation) { - $backupBindings[$relation] = $instance->{$relation}; + if (!empty($instance->__backAssociation[$relation])) { + $backupBindings[$relation] = $instance->__backAssociation[$relation]; + } else { + $backupBindings[$relation] = $instance->{$relation}; + } } foreach ($this->types as $type) { $unbind = array(); diff --git a/cake/tests/cases/libs/model/behaviors/containable.test.php b/cake/tests/cases/libs/model/behaviors/containable.test.php index 1b833e77d..9305a2060 100644 --- a/cake/tests/cases/libs/model/behaviors/containable.test.php +++ b/cake/tests/cases/libs/model/behaviors/containable.test.php @@ -3327,6 +3327,31 @@ class ContainableBehaviorTest extends CakeTestCase { $this->Article->resetBindings(); } +/** + * testResetAddedAssociation method + * + * @access public + */ + function testResetAddedAssociation() { + $this->assertTrue(empty($this->Article->hasMany['ArticlesTag'])); + + $this->Article->bindModel(array( + 'hasMany' => array('ArticlesTag') + )); + $this->assertTrue(!empty($this->Article->hasMany['ArticlesTag'])); + + $result = $this->Article->find('first', array( + 'conditions' => array('Article.id' => 1), + 'contain' => array('ArticlesTag') + )); + $expected = array('Article', 'ArticlesTag'); + $this->assertTrue(!empty($result)); + $this->assertEqual('First Article', $result['Article']['title']); + $this->assertTrue(!empty($result['ArticlesTag'])); + $this->assertEqual($expected, array_keys($result)); + + $this->assertTrue(empty($this->Article->hasMany['ArticlesTag'])); + } /** * testResetAssociation method *