Skip to content

Commit

Permalink
Using Association::find BelongsToMany::unlink()
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Dec 18, 2013
1 parent 3acadff commit ec5a2aa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
6 changes: 3 additions & 3 deletions Cake/ORM/Association/BelongsToMany.php
Expand Up @@ -779,15 +779,15 @@ protected function _collectJointEntities($sourceEntity, $targetEntities) {
}

$belongsTo = $junction->association($target->alias());
$hasMany = $source->association($junction->alias());
$foreignKey = (array)$this->foreignKey();
$assocForeignKey = (array)$belongsTo->foreignKey();
$sourceKey = $sourceEntity->extract((array)$source->primaryKey());

foreach ($missing as $key) {
$unions[] = $junction->find('all')
$unions[] = $hasMany->find('all')
->where(array_combine($foreignKey, $sourceKey))
->andWhere(array_combine($assocForeignKey, $key))
->andWhere($belongsTo->conditions());
->andWhere(array_combine($assocForeignKey, $key));
}

$query = array_shift($unions);
Expand Down
26 changes: 12 additions & 14 deletions Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php
Expand Up @@ -826,10 +826,8 @@ public function testUnlinkSuccess() {
'through' => $joint,
'joinTable' => 'tags_articles'
];
$assoc = new BelongsToMany('Test', $config);
$assoc
->junction()
->association('tags')
$assoc = $this->article->belongsToMany('Test', $config);
$this->article->association('ArticlesTags')
->conditions(['foo' => 1]);

$query1 = $this->getMock('\Cake\ORM\Query', [], [$connection, $joint]);
Expand All @@ -843,34 +841,34 @@ public function testUnlinkSuccess() {
->with('all')
->will($this->returnValue($query2));

$query1->expects($this->once())
$query1->expects($this->at(0))
->method('where')
->with(['article_id' => 1])
->with(['foo' => 1])
->will($this->returnSelf());
$query1->expects($this->at(1))
->method('andWhere')
->with(['tag_id' => 2])
->method('where')
->with(['article_id' => 1])
->will($this->returnSelf());
$query1->expects($this->at(2))
->method('andWhere')
->with(['foo' => 1])
->with(['tag_id' => 2])
->will($this->returnSelf());
$query1->expects($this->once())
->method('union')
->with($query2)
->will($this->returnSelf());

$query2->expects($this->once())
$query2->expects($this->at(0))
->method('where')
->with(['article_id' => 1])
->with(['foo' => 1])
->will($this->returnSelf());
$query2->expects($this->at(1))
->method('andWhere')
->with(['tag_id' => 3])
->method('where')
->with(['article_id' => 1])
->will($this->returnSelf());
$query2->expects($this->at(2))
->method('andWhere')
->with(['foo' => 1])
->with(['tag_id' => 3])
->will($this->returnSelf());

$jointEntities = [
Expand Down

0 comments on commit ec5a2aa

Please sign in to comment.