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...
markstory committed Apr 6, 2010
1 parent 46c4cab commit 629740d59631857996488a5aa420fbe629769450
@@ -182,11 +182,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;
+ }
}
}
}
@@ -2872,23 +2872,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) {
@@ -2925,14 +2936,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(
@@ -3009,7 +3058,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 629740d

Please sign in to comment.