Browse Source

Merge pull request #8888 from TheFRedFox/issue/incorrect-association-finder

Fixed incorrect keys being used by find() in BelongsToMany Association
Mark Story 10 years ago
parent
commit
98b3b0e92a

+ 1 - 1
src/ORM/Association/BelongsToMany.php

@@ -894,7 +894,7 @@ class BelongsToMany extends Association
 
         $belongsTo = $this->junction()->association($this->target()->alias());
         $conditions = $belongsTo->_joinCondition([
-            'foreignKey' => $this->foreignKey()
+            'foreignKey' => $this->targetForeignKey()
         ]);
         $conditions += $this->junctionConditions();
         return $this->_appendJunctionJoin($query, $conditions);

+ 2 - 0
tests/TestCase/ORM/Association/BelongsToManyTest.php

@@ -1022,6 +1022,7 @@ class BelongsToManyTest extends TestCase
         $query = $table->Tags->find();
         $result = $query->toArray();
         $this->assertCount(1, $result);
+        $this->assertEquals(1, $result[0]->id);
     }
 
     /**
@@ -1045,6 +1046,7 @@ class BelongsToManyTest extends TestCase
         $query = $table->Tags->find();
         $result = $query->toArray();
         $this->assertCount(1, $result);
+        $this->assertEquals(1, $result[0]->id);
     }
 
     /**