Permalink
Browse files

Transmitting custom foreignKeys to associations involded in

BelongsToMany when they are customized
  • Loading branch information...
1 parent 6fde319 commit 590415907e90ad7e27f6fabe9a9c38b42c4a5858 @lorenzo lorenzo committed Jan 10, 2014
Showing with 33 additions and 3 deletions.
  1. +11 −3 Cake/ORM/Association/BelongsToMany.php
  2. +22 −0 Test/TestCase/ORM/Association/BelongsToManyTest.php
@@ -166,15 +166,23 @@ public function junction($table = null) {
$junctionAlias = $table->alias();
if (!$table->association($sAlias)) {
- $table->belongsTo($sAlias)->target($source);
+ $table
+ ->belongsTo($sAlias, ['foreignKey' => $this->foreignKey()])
+ ->target($source);
}
if (!$table->association($tAlias)) {
- $table->belongsTo($tAlias)->target($target);
+ $table
+ ->belongsTo($tAlias, ['foreignKey' => $this->targetForeignKey()])
+ ->target($target);
}
if (!$target->association($junctionAlias)) {
- $target->belongsToMany($sAlias);
+ $target->belongsToMany($sAlias, [
+ 'sourceTable' => $source,
+ 'foreignKey' => $this->targetForeignKey(),
+ 'targetForeignKey' => $this->foreignKey()
+ ]);
$target->hasMany($junctionAlias)->target($table);
}
@@ -1316,4 +1316,26 @@ public function testTargetForeignKey() {
$this->assertEquals('foo', $assoc->targetForeignKey());
}
+/**
+ * Tests that custom foreignKeys are properly trasmitted to involved associations
+ * when they are customized
+ *
+ * @return void
+ */
+ public function testJunctionWithCustomForeignKeys() {
+ $assoc = new BelongsToMany('Test', [
+ 'sourceTable' => $this->article,
+ 'targetTable' => $this->tag,
+ 'foreignKey' => 'Art',
+ 'targetForeignKey' => 'Tag'
+ ]);
+ $junction = $assoc->junction();
+ $this->assertEquals('Art', $junction->association('Articles')->foreignKey());
+ $this->assertEquals('Tag', $junction->association('Tags')->foreignKey());
+
+ $inverseRelation = $this->tag->association('Articles');
+ $this->assertEquals('Tag', $inverseRelation->foreignKey());
+ $this->assertEquals('Art', $inverseRelation->targetForeignKey());
+ }
+
}

0 comments on commit 5904159

Please sign in to comment.