diff --git a/cake/libs/model/datasources/dbo_source.php b/cake/libs/model/datasources/dbo_source.php index 1aa56f73d..1ad21c115 100644 --- a/cake/libs/model/datasources/dbo_source.php +++ b/cake/libs/model/datasources/dbo_source.php @@ -858,11 +858,8 @@ class DboSource extends DataSource { foreach ($fetch as $j => $data) { if ( - (isset($data[$with]) && $data[$with][$foreignKey] === $row[$model->alias][$model->primaryKey]) && - (!in_array($data[$with][$joinKeys[1]], $uniqueIds)) + (isset($data[$with]) && $data[$with][$foreignKey] === $row[$model->alias][$model->primaryKey]) ) { - $uniqueIds[] = $data[$with][$joinKeys[1]]; - if ($habtmFieldsCount <= 2) { unset($data[$with]); } diff --git a/cake/tests/cases/libs/model/model.test.php b/cake/tests/cases/libs/model/model.test.php index 72e1d381c..01f5159dd 100644 --- a/cake/tests/cases/libs/model/model.test.php +++ b/cake/tests/cases/libs/model/model.test.php @@ -13022,5 +13022,42 @@ class ModelTest extends CakeTestCase { $TestModel2 =& new ArticleB(); $this->assertEqual($TestModel2->ArticlesTag->primaryKey, 'article_id'); } +/** + * testFetchingNonUniqueFKJoinTableRecords() + * + * Tests if the results are properly returned in the case there are non-unique FK's + * in the join table but another fields value is different. For example: + * something_id | something_else_id | doomed = 1 + * something_id | something_else_id | doomed = 0 + * Should return both records and not just one. + * + * @access public + * @return void + */ + function testFetchingNonUniqueFKJoinTableRecords() { + $this->loadFixtures('Something', 'SomethingElse', 'JoinThing'); + $Something = new Something(); + + $joinThingData = array( + 'JoinThing' => array( + 'something_id' => 1, + 'something_else_id' => 2, + 'doomed' => '0', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + ) + ); + $Something->JoinThing->create($joinThingData); + $Something->JoinThing->save(); + + $result = $Something->JoinThing->find('all', array('conditions' => array('something_else_id' => 2))); + $this->assertEqual($result[0]['JoinThing']['doomed'], 1); + $this->assertEqual($result[1]['JoinThing']['doomed'], 0); + + $result = $Something->find('first'); + $this->assertEqual(count($result['SomethingElse']), 2); + $this->assertEqual($result['SomethingElse'][0]['JoinThing']['doomed'], 1); + $this->assertEqual($result['SomethingElse'][1]['JoinThing']['doomed'], 0); + } } ?> \ No newline at end of file