Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
  • Loading branch information...
commit f2b7a26be8d125fc46fb4f816ae3b0fd5e6e53bf 1 parent 45a51ed
gwoo authored
View
5 cake/libs/model/datasources/dbo_source.php
@@ -858,11 +858,8 @@ function queryAssociation(&$model, &$linkModel, $type, $association, $assocData,
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
37 cake/tests/cases/libs/model/model.test.php
@@ -13022,5 +13022,42 @@ function testPkInHabtmLinkModelArticleB() {
$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);
+ }
}
?>
Please sign in to comment.
Something went wrong with that request. Please try again.