mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
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:
parent
f65f2fd3bc
commit
61415e2449
3 changed files with 56 additions and 9 deletions
|
@ -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]);
|
||||
|
|
|
@ -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() {
|
||||
|
|
12
cake/tests/fixtures/comment_fixture.php
vendored
12
cake/tests/fixtures/comment_fixture.php
vendored
|
@ -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')
|
||||
);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue