From 1a69866df129eb748612d416daf62e77829090f8 Mon Sep 17 00:00:00 2001 From: nate Date: Fri, 7 Mar 2008 04:36:20 +0000 Subject: [PATCH] Fixing custom hasMany associations, fixes #3918 git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6512 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/model/datasources/dbo_source.php | 13 ++-- cake/tests/cases/libs/model/model.test.php | 78 ++++++++++++---------- 2 files changed, 51 insertions(+), 40 deletions(-) diff --git a/cake/libs/model/datasources/dbo_source.php b/cake/libs/model/datasources/dbo_source.php index b2060bf83..ec891e4e9 100644 --- a/cake/libs/model/datasources/dbo_source.php +++ b/cake/libs/model/datasources/dbo_source.php @@ -687,7 +687,7 @@ class DboSource extends DataSource { } $count = count($resultSet); - if ($type === 'hasMany' && (!isset($assocData['limit']) || empty($assocData['limit']))) { + if ($type === 'hasMany' && empty($assocData['limit']) && !empty($assocData['foreignKey'])) { $ins = $fetch = array(); for ($i = 0; $i < $count; $i++) { if ($in = $this->insertQueryData('{$__cakeID__$}', $resultSet[$i], $association, $assocData, $model, $linkModel, $stack)) { @@ -1119,11 +1119,12 @@ class DboSource extends DataSource { } break; case 'hasMany': - $assocData['fields'] = array_unique(array_merge( - $this->fields($linkModel, $alias, $assocData['fields']), - $this->fields($linkModel, $alias, array("{$alias}.{$assocData['foreignKey']}")) - )); - + $assocData['fields'] = $this->fields($linkModel, $alias, $assocData['fields']); + if (!empty($assocData['foreignKey'])) { + $assocData['fields'] = array_unique(array_merge( + $assocData['fields'], $this->fields($linkModel, $alias, array("{$alias}.{$assocData['foreignKey']}")) + )); + } $query = array( 'conditions' => $this->__mergeConditions($this->getConstraint('hasMany', $model, $linkModel, $alias, $assocData), $assocData['conditions']), 'fields' => $assocData['fields'], diff --git a/cake/tests/cases/libs/model/model.test.php b/cake/tests/cases/libs/model/model.test.php index 45d308217..48230dc82 100644 --- a/cake/tests/cases/libs/model/model.test.php +++ b/cake/tests/cases/libs/model/model.test.php @@ -220,6 +220,42 @@ class ModelTest extends CakeTestCase { $this->assertEqual($result, $expected); } + function testDynamicAssociations() { + $this->loadFixtures('Article', 'Comment'); + $this->model =& new Article(); + + $this->model->belongsTo = $this->model->hasAndBelongsToMany = $this->model->hasOne = array(); + $this->model->hasMany['Comment'] = am($this->model->hasMany['Comment'], array( + 'foreignKey' => false, + 'conditions' => array('Comment.user_id' => '= 2') + )); + $result = $this->model->find('all'); + $expected = array( + array( + 'Article' => array('id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'), + 'Comment' => array( + 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' => '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') + ) + ), + array( + 'Article' => array('id' => '2', 'user_id' => '3', 'title' => 'Second Article', 'body' => 'Second Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31'), + 'Comment' => array( + 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' => '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') + ) + ), + array( + 'Article' => array('id' => '3', 'user_id' => '1', 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'), + 'Comment' => array( + 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' => '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') + ) + ) + ); + $this->assertEqual($result, $expected); + } + function testSaveMultipleHabtm() { $this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC'); $this->model = new JoinA(); @@ -1760,18 +1796,9 @@ class ModelTest extends CakeTestCase { // Parent data after HABTM data - $data = array( - 'Tag' => array( - 'Tag' => array( 1, 2 ) - ), - 'Article' => array('id' => '2', 'title' => 'New Second Article' ), - ); - - $result = $this->model->set($data); - $this->assertTrue($result); - - $result = $this->model->save(); - $this->assertTrue($result); + $data = array('Tag' => array('Tag' => array(1, 2)), 'Article' => array('id' => '2', 'title' => 'New Second Article')); + $this->assertTrue($this->model->set($data)); + $this->assertTrue($this->model->save()); $this->model->unbindModel(array( 'belongsTo' => array('User'), @@ -1789,18 +1816,10 @@ class ModelTest extends CakeTestCase { ); $this->assertEqual($result, $expected); - $data = array( - 'Tag' => array( - 'Tag' => array( 1, 2 ) - ), - 'Article' => array('id' => '2', 'title' => 'New Second Article Title' ), - ); - + $data = array('Tag' => array('Tag' => array(1, 2)), 'Article' => array('id' => '2', 'title' => 'New Second Article Title')); $result = $this->model->set($data); $this->assertTrue($result); - - $result = $this->model->save(); - $this->assertTrue($result); + $this->assertTrue($this->model->save()); $this->model->unbindModel(array( 'belongsTo' => array('User'), @@ -1818,18 +1837,9 @@ class ModelTest extends CakeTestCase { ); $this->assertEqual($result, $expected); - $data = array( - 'Tag' => array( - 'Tag' => array( 2, 3 ) - ), - 'Article' => array('id' => '2', 'title' => 'Changed Second Article' ), - ); - - $result = $this->model->set($data); - $this->assertTrue($result); - - $result = $this->model->save(); - $this->assertTrue($result); + $data = array('Tag' => array('Tag' => array(2, 3)), 'Article' => array('id' => '2', 'title' => 'Changed Second Article')); + $this->assertTrue($this->model->set($data)); + $this->assertTrue($this->model->save()); $this->model->unbindModel(array( 'belongsTo' => array('User'),