Skip to content

Commit

Permalink
Fixing discrepancy between how belongsTo and hasOne assocations are t…
Browse files Browse the repository at this point in the history
…reated in relation to their fields being added into the queryData. hasOne and belongsTo associations now behave the same. Fixes #379
  • Loading branch information
markstory committed Apr 6, 2010
1 parent dde5239 commit 518cab9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 10 deletions.
12 changes: 7 additions & 5 deletions cake/libs/model/behaviors/containable.php
Expand Up @@ -183,11 +183,13 @@ function beforeFind(&$Model, $query) {
}

$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;
}
}
}
}
Expand Down
65 changes: 60 additions & 5 deletions cake/tests/cases/libs/model/behaviors/containable.test.php
Expand Up @@ -2883,23 +2883,34 @@ function testFindEmbeddedThirdLevelNonReset() {
* @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)),
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')));
$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)),
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')));
$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) {
Expand Down Expand Up @@ -2937,14 +2948,52 @@ function testEmbeddedFindFields() {
$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
*
* @access public
* @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(
Expand Down Expand Up @@ -3021,7 +3070,13 @@ function testFindConditionalBinding() {
);
$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(
Expand Down

0 comments on commit 518cab9

Please sign in to comment.