Skip to content
Permalink
Browse files

Adding test to prove attachTo in BelongsToMany works with composite

primary keys
  • Loading branch information...
lorenzo committed Jan 10, 2014
1 parent 5904159 commit 9a4d3c9784c8a94aba19da86c303945c787847c8
Showing with 49 additions and 1 deletion.
  1. +2 −1 Cake/ORM/Association/BelongsToMany.php
  2. +47 −0 Test/TestCase/ORM/Association/BelongsToManyTest.php
@@ -222,7 +222,8 @@ public function attachTo(Query $query, array $options = []) {
unset($options['queryBuilder']);
$options = ['conditions' => [$cond]] + compact('includeFields');
$this->target()
$options['foreignKey'] = $this->targetForeignKey();
$this->_targetTable
->association($junction->alias())
->attachTo($query, $options);
}
@@ -361,6 +361,53 @@ public function testAttachToWithQueryBuilder() {
]);
}
/**
* Tests that using belongsToMany with a table having a multi column primary
* key will work if the foreign key is passed
*
* @return void
*/
public function testAttachToMultiPrimaryKey() {
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'sourceTable' => $this->article,
'targetTable' => $this->tag,
'conditions' => ['Tags.name' => 'cake'],
'foreignKey' => ['article_id', 'article_site_id'],
'targetForeignKey' => ['tag_id', 'tag_site_id']
];
$this->article->primaryKey(['id', 'site_id']);
$this->tag->primaryKey(['id', 'my_site_id']);
$association = new BelongsToMany('Tags', $config);
$query->expects($this->at(0))->method('join')->with([
'Tags' => [
'conditions' => new QueryExpression([
'Tags.name' => 'cake'
]),
'type' => 'INNER',
'table' => 'tags'
]
]);
$fieldA = new IdentifierExpression('ArticlesTags.article_id');
$fieldB = new IdentifierExpression('ArticlesTags.article_site_id');
$fieldC = new IdentifierExpression('ArticlesTags.tag_id');
$fieldD = new IdentifierExpression('ArticlesTags.tag_site_id');
$query->expects($this->at(1))->method('join')->with([
'ArticlesTags' => [
'conditions' => new QueryExpression([
['Articles.id' => $fieldA, 'Articles.site_id' => $fieldB],
['Tags.id' => $fieldC, 'Tags.my_site_id' => $fieldD]
]),
'type' => 'INNER',
'table' => 'articles_tags'
]
]);
$query->expects($this->never())->method('select');
$association->attachTo($query, ['includeFields' => false]);
}
/**
* Test the eager loader method with no extra options
*

0 comments on commit 9a4d3c9

Please sign in to comment.
You can’t perform that action at this time.