Skip to content

Commit

Permalink
Fixes bug where the junction table instance was not the same object for
Browse files Browse the repository at this point in the history
the same association.
  • Loading branch information
lorenzo committed Jun 5, 2014
1 parent 2b8ab51 commit fd92e96
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ORM/Association/BelongsToMany.php
Expand Up @@ -184,7 +184,8 @@ public function junction($table = null) {
$target->belongsToMany($sAlias, [
'sourceTable' => $source,
'foreignKey' => $this->targetForeignKey(),
'targetForeignKey' => $this->foreignKey()
'targetForeignKey' => $this->foreignKey(),
'through' => $table
]);
$target->hasMany($junctionAlias, [
'targetTable' => $table,
Expand Down
18 changes: 18 additions & 0 deletions tests/TestCase/ORM/QueryRegressionTest.php
Expand Up @@ -158,4 +158,22 @@ public function testCreateJointData() {
$this->assertEquals('2014-06-01', $entity->highlights[0]->_joinData->highlighted_time->format('Y-m-d'));
}

/**
* Tests that the juction table instance taken from both sides of a belongsToMany
* relationship is actually the same object.
*
* @return void
*/
public function testReciprocalBelongsToMany() {
$articles = TableRegistry::get('Articles');
$tags = TableRegistry::get('Tags');

$articles->belongsToMany('Tags');
$tags->belongsToMany('Articles');

$left = $articles->Tags->junction();
$right = $tags->Articles->junction();
$this->assertSame($left, $right);
}

}

0 comments on commit fd92e96

Please sign in to comment.