fixes #5710, HABTM - constraining unique ids, but removing non-unique

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8217 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2009-07-03 03:31:05 +00:00
parent 45a51ed809
commit f2b7a26be8
2 changed files with 38 additions and 4 deletions

View file

@ -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]);
}

View file

@ -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);
}
}
?>