diff --git a/cake/libs/model/behaviors/containable.php b/cake/libs/model/behaviors/containable.php index fd5cc56e9..ac9891724 100644 --- a/cake/libs/model/behaviors/containable.php +++ b/cake/libs/model/behaviors/containable.php @@ -265,10 +265,12 @@ class ContainableBehavior extends ModelBehavior { $children[$key] = (array)$val; } } + $keys = array_keys($children); if ($keys && isset($children[0])) { - $keys = array_values($children); + $keys = am(array_values($children), $keys); } + foreach ($keys as $i => $key) { if (is_array($key)) { continue; @@ -291,7 +293,7 @@ class ContainableBehavior extends ModelBehavior { $key = $option; $optionKey = true; } - if ($optionKey) { + if ($optionKey && isset($children[$key])) { $keep[$name][$key] = array_merge((isset($keep[$name][$key]) ? $keep[$name][$key] : array()), (array) $children[$key]); unset($children[$key]); } diff --git a/cake/tests/cases/libs/model/behaviors/containable.test.php b/cake/tests/cases/libs/model/behaviors/containable.test.php index 10a84c51d..4e6d1f959 100644 --- a/cake/tests/cases/libs/model/behaviors/containable.test.php +++ b/cake/tests/cases/libs/model/behaviors/containable.test.php @@ -85,6 +85,16 @@ class ContainableTest extends CakeTestCase { ) ))); $this->assertEqual(Set::extract('/ArticleFeatured/keep/Featured/fields', $r), array('id')); + + $r = $this->__containments($this->Article, array( + 'Comment' => array( + 'User', + 'conditions' => array('Comment' => array('user_id' => 2)), + ), + )); + $this->assertTrue(Set::matches('/User', $r)); + $this->assertTrue(Set::matches('/Comment', $r)); + $this->assertTrue(Set::matches('/Article/keep/Comment/conditions/Comment[user_id=2]', $r)); } function testInvalidContainments() { @@ -172,17 +182,6 @@ class ContainableTest extends CakeTestCase { $r = $this->Article->find('all'); $this->assertFalse(Set::matches('/Comment/User', $r)); - - $this->Article->contain(array( - 'User', - 'Comment' => array( - 'conditions' => array('Comment' => array('user_id' => '!=2')), - ), - 'Tag' - )); - $r = $this->Article->find('all'); - $this->assertFalse(Set::matches('/Comment[user_id=2]', $r)); - $this->assertTrue(Set::matches('/Comment[user_id!=2]', $r)); } function testFindEmbeddedNoBindings() {