Allowing children when Model(field1, field2, ...) is used as key, fixes #4690

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6989 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
mariano.iglesias 2008-05-20 15:21:38 +00:00
parent f65f2fd3bc
commit 61415e2449
3 changed files with 56 additions and 9 deletions

View file

@ -297,9 +297,16 @@ class ContainableBehavior extends ModelBehavior {
$val = $Model->{$name}->alias.'.'.$key; $val = $Model->{$name}->alias.'.'.$key;
} }
$children[$option] = isset($children[$option]) ? array_merge((array) $children[$option], (array) $val) : $val; $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]); unset($children[$key], $children[$i]);
$key = $option; $key = $option;
$optionKey = true; $optionKey = true;
if (!empty($newChildren)) {
$children = Set::merge($children, $newChildren);
}
} }
if ($optionKey && isset($children[$key])) { if ($optionKey && isset($children[$key])) {
$keep[$name][$key] = array_merge((isset($keep[$name][$key]) ? $keep[$name][$key] : array()), (array) $children[$key]); $keep[$name][$key] = array_merge((isset($keep[$name][$key]) ? $keep[$name][$key] : array()), (array) $children[$key]);

View file

@ -95,6 +95,16 @@ class ContainableTest extends CakeTestCase {
$this->assertTrue(Set::matches('/User', $r)); $this->assertTrue(Set::matches('/User', $r));
$this->assertTrue(Set::matches('/Comment', $r)); $this->assertTrue(Set::matches('/Comment', $r));
$this->assertTrue(Set::matches('/Article/keep/Comment/conditions/Comment[user_id=2]', $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() { function testInvalidContainments() {
@ -2766,6 +2776,36 @@ class ContainableTest extends CakeTestCase {
array('Article' => array('title' => 'Third Article'), 'User' => array('user' => 'mariano', 'id' => 1)), array('Article' => array('title' => 'Third Article'), 'User' => array('user' => 'mariano', 'id' => 1)),
); );
$this->assertEqual($result, $expected); $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() { function testFindConditionalBinding() {

View file

@ -44,12 +44,12 @@ class CommentFixture extends CakeTestFixture {
'updated' => 'datetime' 'updated' => 'datetime'
); );
var $records = array( 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('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('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' => 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('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' => 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('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' => 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('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' => 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('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' => 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')
); );
} }