Skip to content
Permalink
Browse files

Refectoring unlink and adding more tests

  • Loading branch information...
lorenzo committed Dec 14, 2013
1 parent d5ab25c commit 92edc0da4aee454d4fd422c3d8c0d863df93e777
Showing with 54 additions and 8 deletions.
  1. +3 −8 Cake/ORM/Association/BelongsToMany.php
  2. +51 −0 Cake/Test/TestCase/ORM/TableTest.php
@@ -461,6 +461,7 @@ protected function _collectJointEntities($sourceEntity, $targetEntities) {
$source = $this->source();
$junction = $this->junction();
$jointProperty = $target->association($junction->alias())->property();
$primary = (array)$target->primaryKey();
$result = [];
$missing = [];
@@ -469,7 +470,7 @@ protected function _collectJointEntities($sourceEntity, $targetEntities) {
$joint = $entity->get($jointProperty);
if (!$joint) {
$missing[] = $entity;
$missing[] = $entity->extract($primary);
continue;
}
@@ -481,18 +482,12 @@ protected function _collectJointEntities($sourceEntity, $targetEntities) {
return $result;
}
$primary = (array)$target->primaryKey();
$keys = array_map(function($e) use ($primary) {
return $e->extract($primary);
}, $missing);
$conditions = [];
$belongsTo = $junction->association($target->alias());
$foreignKey = (array)$this->foreignKey();
$assocForeignKey = (array)$belongsTo->foreignKey();
$sourceKey = $sourceEntity->extract((array)$source->primaryKey());
foreach($keys as $key) {
foreach($missing as $key) {
$unions[] = $junction->find('all')
->where(array_combine($foreignKey, $sourceKey))
->andWhere(array_combine($assocForeignKey, $key))
@@ -2798,6 +2798,11 @@ public function testLinkBelongsToMany() {
$this->assertEquals($article->tags[3], $tags[1]);
}
/**
* Integration test to show how to unlink a single record from a belongsToMany
*
* @return void
*/
public function testUnlinkBelongsToMany() {
$table = TableRegistry::get('articles');
$table->belongsToMany('tags');
@@ -2813,4 +2818,50 @@ public function testUnlinkBelongsToMany() {
$this->assertEquals(2, $left->tags[0]->get('id'));
}
/**
* Integration test to show how to unlink multiple records from a belongsToMany
*
* @return void
*/
public function testUnlinkBelongsToManyMultiple() {
$table = TableRegistry::get('articles');
$table->belongsToMany('tags');
$tagsTable = TableRegistry::get('tags');
$options = ['markNew' => false];
$article = new \Cake\ORM\Entity(['id' => 1,], $options);
$tags[] = new \TestApp\Model\Entity\Tag(['id' => 1], $options);
$tags[] = new \TestApp\Model\Entity\Tag(['id' => 2], $options);
$table->association('tags')->unlink($article, $tags);
$left = $table->find('all')->where(['id' => 1])->contain(['tags'])->first();
$this->assertNull($left->tags);
}
/**
* Integration test to show how to unlink multiple records from a belongsToMany
* providing some of the joint
*
* @return void
*/
public function testUnlinkBelongsToManyPassingJoint() {
$table = TableRegistry::get('articles');
$table->belongsToMany('tags');
$tagsTable = TableRegistry::get('tags');
$options = ['markNew' => false];
$article = new \Cake\ORM\Entity(['id' => 1,], $options);
$tags[] = new \TestApp\Model\Entity\Tag(['id' => 1], $options);
$tags[] = new \TestApp\Model\Entity\Tag(['id' => 2], $options);
$tags[1]->extraInfo = new \Cake\ORM\Entity([
'article_id' => 1,
'tag_id' => 2
]);
$table->association('tags')->unlink($article, $tags);
$left = $table->find('all')->where(['id' => 1])->contain(['tags'])->first();
$this->assertNull($left->tags);
}
}

0 comments on commit 92edc0d

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