Permalink
Browse files

Fixing discrepancy between how belongsTo and hasOne assocations are t…

…reated in relation to their fields being added into the queryData. hasOne and belongsTo associations now behave the same. Fixes #379
  • Loading branch information...
1 parent dde5239 commit 518cab91e58c9dc1c18d8d629e152b8a8903f852 @markstory markstory committed Apr 6, 2010
@@ -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;
+ }
}
}
}
@@ -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) {
@@ -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(
@@ -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(

0 comments on commit 518cab9

Please sign in to comment.