mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Fixing discrepancy between how belongsTo and hasOne assocations are treated in relation to their fields being added into the queryData. hasOne and belongsTo associations now behave the same. Fixes #379
This commit is contained in:
parent
dde52399ca
commit
518cab91e5
2 changed files with 67 additions and 10 deletions
|
@ -183,11 +183,13 @@ class ContainableBehavior extends ModelBehavior {
|
|||
}
|
||||
|
||||
$query['fields'] = (array)$query['fields'];
|
||||
if (!empty($Model->belongsTo)) {
|
||||
foreach ($Model->belongsTo as $assoc => $data) {
|
||||
if (!empty($data['fields'])) {
|
||||
foreach ((array) $data['fields'] as $field) {
|
||||
$query['fields'][] = (strpos($field, '.') === false ? $assoc . '.' : '') . $field;
|
||||
foreach (array('hasOne', 'belongsTo') as $type) {
|
||||
if (!empty($Model->{$type})) {
|
||||
foreach ($Model->{$type} as $assoc => $data) {
|
||||
if (!empty($data['fields'])) {
|
||||
foreach ((array) $data['fields'] as $field) {
|
||||
$query['fields'][] = (strpos($field, '.') === false ? $assoc . '.' : '') . $field;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2883,7 +2883,10 @@ class ContainableBehaviorTest extends CakeTestCase {
|
|||
* @return void
|
||||
*/
|
||||
function testEmbeddedFindFields() {
|
||||
$result = $this->Article->find('all', array('contain' => array('User(user)'), 'fields' => array('title')));
|
||||
$result = $this->Article->find('all', array(
|
||||
'contain' => array('User(user)'),
|
||||
'fields' => array('title')
|
||||
));
|
||||
$expected = array(
|
||||
array('Article' => array('title' => 'First Article'), 'User' => array('user' => 'mariano', 'id' => 1)),
|
||||
array('Article' => array('title' => 'Second Article'), 'User' => array('user' => 'larry', 'id' => 3)),
|
||||
|
@ -2891,7 +2894,10 @@ class ContainableBehaviorTest extends CakeTestCase {
|
|||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->Article->find('all', array('contain' => array('User(id, user)'), 'fields' => array('title')));
|
||||
$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)),
|
||||
array('Article' => array('title' => 'Second Article'), 'User' => array('user' => 'larry', 'id' => 3)),
|
||||
|
@ -2899,7 +2905,12 @@ class ContainableBehaviorTest extends CakeTestCase {
|
|||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->Article->find('all', array('contain' => array('Comment(comment, published)' => 'Attachment(attachment)', 'User(user)'), 'fields' => array('title')));
|
||||
$result = $this->Article->find('all', array(
|
||||
'contain' => array(
|
||||
'Comment(comment, published)' => 'Attachment(attachment)', 'User(user)'
|
||||
),
|
||||
'fields' => array('title')
|
||||
));
|
||||
if (!empty($result)) {
|
||||
foreach($result as $i=>$article) {
|
||||
foreach($article['Comment'] as $j=>$comment) {
|
||||
|
@ -2937,6 +2948,38 @@ class ContainableBehaviorTest extends CakeTestCase {
|
|||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* test that hasOne and belongsTo fields act the same in a contain array.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testHasOneFieldsInContain() {
|
||||
$this->Article->unbindModel(array(
|
||||
'hasMany' => array('Comment')
|
||||
), true);
|
||||
unset($this->Article->Comment);
|
||||
$this->Article->bindModel(array(
|
||||
'hasOne' => array('Comment')
|
||||
));
|
||||
|
||||
$result = $this->Article->find('all', array(
|
||||
'fields' => array('title', 'body'),
|
||||
'contain' => array(
|
||||
'Comment' => array(
|
||||
'fields' => array('comment')
|
||||
),
|
||||
'User' => array(
|
||||
'fields' => array('user')
|
||||
)
|
||||
)
|
||||
));
|
||||
$this->assertTrue(isset($result[0]['Article']['title']), 'title missing %s');
|
||||
$this->assertTrue(isset($result[0]['Article']['body']), 'body missing %s');
|
||||
$this->assertTrue(isset($result[0]['Comment']['comment']), 'comment missing %s');
|
||||
$this->assertTrue(isset($result[0]['User']['user']), 'body missing %s');
|
||||
$this->assertFalse(isset($result[0]['Comment']['published']), 'published found %s');
|
||||
$this->assertFalse(isset($result[0]['User']['password']), 'password found %s');
|
||||
}
|
||||
/**
|
||||
* testFindConditionalBinding method
|
||||
*
|
||||
|
@ -2944,7 +2987,13 @@ class ContainableBehaviorTest extends CakeTestCase {
|
|||
* @return void
|
||||
*/
|
||||
function testFindConditionalBinding() {
|
||||
$this->Article->contain(array('User(user)', 'Tag' => array('fields' => array('tag', 'created'), 'conditions' => array('created >=' => '2007-03-18 12:24'))));
|
||||
$this->Article->contain(array(
|
||||
'User(user)',
|
||||
'Tag' => array(
|
||||
'fields' => array('tag', 'created'),
|
||||
'conditions' => array('created >=' => '2007-03-18 12:24')
|
||||
)
|
||||
));
|
||||
$result = $this->Article->find('all', array('fields' => array('title')));
|
||||
$expected = array(
|
||||
array(
|
||||
|
@ -3021,7 +3070,13 @@ class ContainableBehaviorTest extends CakeTestCase {
|
|||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$this->Article->contain(array('User(id,user)', 'Tag' => array('fields' => array('tag', 'created'), 'conditions' => array('created >=' => '2007-03-18 12:24'))));
|
||||
$this->Article->contain(array(
|
||||
'User(id,user)',
|
||||
'Tag' => array(
|
||||
'fields' => array('tag', 'created'),
|
||||
'conditions' => array('created >=' => '2007-03-18 12:24')
|
||||
)
|
||||
));
|
||||
$result = $this->Article->find('all', array('fields' => array('title')));
|
||||
$expected = array(
|
||||
array(
|
||||
|
|
Loading…
Reference in a new issue