### Options
Additionally to the default options accepted by Table::delete(), the following
keys are supported:
- cleanProperty: Whether or not to remove all the objects in $targetEntities that
are stored in $sourceEntity (default: true)
By default this method will unset each of the entity objects stored inside the
source entity.
### Example:
$article->tags = [$tag1, $tag2, $tag3, $tag4];
$tags = [$tag1, $tag2, $tag3];
$articles->association('tags')->unlink($article, $tags);
$article->get('tags') will contain only [$tag4] after deleting in the database
public unlink ( Cake\Datasource\EntityInterface $sourceEntity, array $targetEntities, array | boolean $options = [] ) : boolean | ||
$sourceEntity | Cake\Datasource\EntityInterface | an entity persisted in the source table for this association |
$targetEntities | array | list of entities persisted in the target table for this association |
$options | array | boolean | list of options to be passed to the internal `delete` call, or a `boolean` |
리턴 | boolean | Success |
/** * Test liking entities having a non persited target entity * * @expectedException \InvalidArgumentException * @expectedExceptionMessage Cannot link not persisted entities * @return void */ public function testUnlinkWithNotPersistedTarget() { $config = ['sourceTable' => $this->article, 'targetTable' => $this->tag, 'joinTable' => 'tags_articles']; $assoc = new BelongsToMany('Test', $config); $entity = new Entity(['id' => 1], ['markNew' => false]); $tags = [new Entity(['id' => 2]), new Entity(['id' => 3])]; $assoc->unlink($entity, $tags); }
/** * Tests that unlinking with last parameter set to false * will not remove entities from the association property * * @return void */ public function testUnlinkWithoutPropertyClean() { $connection = ConnectionManager::get('test'); $joint = $this->getMock('\\Cake\\ORM\\Table', ['delete', 'find'], [['alias' => 'ArticlesTags', 'connection' => $connection]]); $config = ['sourceTable' => $this->article, 'targetTable' => $this->tag, 'through' => $joint, 'joinTable' => 'tags_articles']; $assoc = new BelongsToMany('Test', $config); $assoc->junction()->association('tags')->conditions(['foo' => 1]); $joint->expects($this->never())->method('find'); $opts = ['markNew' => false]; $jointEntities = [new Entity(['article_id' => 1, 'tag_id' => 2]), new Entity(['article_id' => 1, 'tag_id' => 3])]; $tags = [new Entity(['id' => 2, '_joinData' => $jointEntities[0]], $opts), new Entity(['id' => 3, '_joinData' => $jointEntities[1]], $opts)]; $entity = new Entity(['id' => 1, 'test' => $tags], $opts); $joint->expects($this->at(0))->method('delete')->with($jointEntities[0]); $joint->expects($this->at(1))->method('delete')->with($jointEntities[1]); $assoc->unlink($entity, $tags, false); $this->assertEquals($tags, $entity->get('test')); }