diff --git a/cake/libs/model/behaviors/containable.php b/cake/libs/model/behaviors/containable.php index b9e5c1a8f..677bb53ce 100644 --- a/cake/libs/model/behaviors/containable.php +++ b/cake/libs/model/behaviors/containable.php @@ -61,7 +61,7 @@ class ContainableBehavior extends ModelBehavior { * * - autoFields: (boolean, optional) auto-add needed fields to fetch requested * bindings. DEFAULTS TO: true - * + * * - countReset: (boolean, optional) If set to false, count queries will not reset containments * like normal queries would. Useful for using contain() and pagination. * DEFAULTS TO: false @@ -297,9 +297,16 @@ class ContainableBehavior extends ModelBehavior { $val = $Model->{$name}->alias.'.'.$key; } $children[$option] = isset($children[$option]) ? array_merge((array) $children[$option], (array) $val) : $val; + $newChildren = null; + if (!empty($name) && !empty($children[$key])) { + $newChildren = $children[$key]; + } unset($children[$key], $children[$i]); $key = $option; $optionKey = true; + if (!empty($newChildren)) { + $children = Set::merge($children, $newChildren); + } } if ($optionKey && isset($children[$key])) { $keep[$name][$key] = array_merge((isset($keep[$name][$key]) ? $keep[$name][$key] : array()), (array) $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 17c5dd551..4085b14d1 100644 --- a/cake/tests/cases/libs/model/behaviors/containable.test.php +++ b/cake/tests/cases/libs/model/behaviors/containable.test.php @@ -95,6 +95,16 @@ class ContainableTest extends CakeTestCase { $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)); + + $r = $this->__containments($this->Article, array('Comment(comment, published)' => 'Attachment(attachment)', 'User(user)')); + $this->assertTrue(Set::matches('/Comment', $r)); + $this->assertTrue(Set::matches('/User', $r)); + $this->assertTrue(Set::matches('/Article/keep/Comment', $r)); + $this->assertTrue(Set::matches('/Article/keep/User', $r)); + $this->assertEqual(Set::extract('/Article/keep/Comment/fields', $r), array('comment', 'published')); + $this->assertEqual(Set::extract('/Article/keep/User/fields', $r), array('user')); + $this->assertTrue(Set::matches('/Comment/keep/Attachment', $r)); + $this->assertEqual(Set::extract('/Comment/keep/Attachment/fields', $r), array('attachment')); } function testInvalidContainments() { @@ -2749,7 +2759,7 @@ class ContainableTest extends CakeTestCase { $this->__assertBindings($this->User->ArticleFeatured->Featured, array('belongsTo' => array('ArticleFeatured', 'Category'))); $this->__assertBindings($this->User->ArticleFeatured->Comment, array('belongsTo' => array('Article', 'User'), 'hasOne' => array('Attachment'))); } - + function testEmbeddedFindFields() { $result = $this->Article->find('all', array('contain' => array('User(user)'), 'fields' => array('title'))); $expected = array( @@ -2758,7 +2768,7 @@ class ContainableTest extends CakeTestCase { array('Article' => array('title' => 'Third Article'), 'User' => array('user' => 'mariano', 'id' => 1)), ); $this->assertEqual($result, $expected); - + $result = $this->Article->find('all', array('contain' => array('User(id, user)'), 'fields' => array('title'))); $expected = array( array('Article' => array('title' => 'First Article'), 'User' => array('user' => 'mariano', 'id' => 1)), @@ -2766,6 +2776,36 @@ class ContainableTest extends CakeTestCase { array('Article' => array('title' => 'Third Article'), 'User' => array('user' => 'mariano', 'id' => 1)), ); $this->assertEqual($result, $expected); + + $result = $this->Article->find('all', array('contain' => array('Comment(comment, published)' => 'Attachment(attachment)', 'User(user)'), 'fields' => array('title'))); + $expected = array( + array( + 'Article' => array('title' => 'First Article', 'id' => 1), + 'User' => array('user' => 'mariano', 'id' => 1), + 'Comment' => array( + array('comment' => 'First Comment for First Article', 'published' => 'Y', 'id' => 1, 'article_id' => 1, 'Attachment' => array()), + array('comment' => 'Second Comment for First Article', 'published' => 'Y', 'id' => 2, 'article_id' => 1, 'Attachment' => array()), + array('comment' => 'Third Comment for First Article', 'published' => 'Y', 'id' => 3, 'article_id' => 1, 'Attachment' => array()), + array('comment' => 'Fourth Comment for First Article', 'published' => 'N', 'id' => 4, 'article_id' => 1, 'Attachment' => array()), + ) + ), + array( + 'Article' => array('title' => 'Second Article', 'id' => 2), + 'User' => array('user' => 'larry', 'id' => 3), + 'Comment' => array( + array('comment' => 'First Comment for Second Article', 'published' => 'Y', 'id' => 5, 'article_id' => 2, 'Attachment' => array( + 'attachment' => 'attachment.zip', 'id' => 1 + )), + array('comment' => 'Second Comment for Second Article', 'published' => 'Y', 'id' => 6, 'article_id' => 2, 'Attachment' => array()) + ) + ), + array( + 'Article' => array('title' => 'Third Article', 'id' => 3), + 'User' => array('user' => 'mariano', 'id' => 1), + 'Comment' => array() + ), + ); + $this->assertEqual($result, $expected); } function testFindConditionalBinding() { diff --git a/cake/tests/fixtures/comment_fixture.php b/cake/tests/fixtures/comment_fixture.php index 329aa1068..f727341c4 100644 --- a/cake/tests/fixtures/comment_fixture.php +++ b/cake/tests/fixtures/comment_fixture.php @@ -44,12 +44,12 @@ class CommentFixture extends CakeTestFixture { 'updated' => 'datetime' ); var $records = array( - array('article_id' => 1, 'user_id' => 2, 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:45:23', 'updated' => '2007-03-18 10:47:31'), - array('article_id' => 1, 'user_id' => 4, 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'), - array('article_id' => 1, 'user_id' => 1, 'comment' => 'Third Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:49:23', 'updated' => '2007-03-18 10:51:31'), - array('article_id' => 1, 'user_id' => 1, 'comment' => 'Fourth Comment for First Article', 'published' => 'N', 'created' => '2007-03-18 10:51:23', 'updated' => '2007-03-18 10:53:31'), - array('article_id' => 2, 'user_id' => 1, 'comment' => 'First Comment for Second Article', 'published' => 'Y', 'created' => '2007-03-18 10:53:23', 'updated' => '2007-03-18 10:55:31'), - array('article_id' => 2, 'user_id' => 2, 'comment' => 'Second Comment for Second Article', 'published' => 'Y', 'created' => '2007-03-18 10:55:23', 'updated' => '2007-03-18 10:57:31') + array('id' => 1, 'article_id' => 1, 'user_id' => 2, 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:45:23', 'updated' => '2007-03-18 10:47:31'), + array('id' => 2, 'article_id' => 1, 'user_id' => 4, 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'), + array('id' => 3, 'article_id' => 1, 'user_id' => 1, 'comment' => 'Third Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:49:23', 'updated' => '2007-03-18 10:51:31'), + array('id' => 4, 'article_id' => 1, 'user_id' => 1, 'comment' => 'Fourth Comment for First Article', 'published' => 'N', 'created' => '2007-03-18 10:51:23', 'updated' => '2007-03-18 10:53:31'), + array('id' => 5, 'article_id' => 2, 'user_id' => 1, 'comment' => 'First Comment for Second Article', 'published' => 'Y', 'created' => '2007-03-18 10:53:23', 'updated' => '2007-03-18 10:55:31'), + array('id' => 6, 'article_id' => 2, 'user_id' => 2, 'comment' => 'Second Comment for Second Article', 'published' => 'Y', 'created' => '2007-03-18 10:55:23', 'updated' => '2007-03-18 10:57:31') ); }