From 8d58b40642c182afe307d00debf44a1dbd8a3c62 Mon Sep 17 00:00:00 2001 From: Mariano Iglesias Date: Sun, 14 Mar 2010 16:34:57 -0300 Subject: [PATCH] Fixing issue in Containable where if bindModel was used to add / change a binding not permanently, Containable was making the change permanent --- cake/libs/model/behaviors/containable.php | 6 ++++- .../libs/model/behaviors/containable.test.php | 25 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/cake/libs/model/behaviors/containable.php b/cake/libs/model/behaviors/containable.php index 7aa02f55e79..8ace8760f1f 100644 --- a/cake/libs/model/behaviors/containable.php +++ b/cake/libs/model/behaviors/containable.php @@ -129,7 +129,11 @@ function beforeFind(&$Model, $query) { 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 ebfa57a5441..a2a6fdc7187 100644 --- a/cake/tests/cases/libs/model/behaviors/containable.test.php +++ b/cake/tests/cases/libs/model/behaviors/containable.test.php @@ -3311,6 +3311,31 @@ function testOriginalAssociations() { $this->assertTrue(Set::matches('/Comment[article_id=1]', $result)); $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 *