public function unlink(EntityInterface $sourceEntity, array $targetEntities, $options = [])
{
if (is_bool($options)) {
$options = ['cleanProperty' => $options];
} else {
$options += ['cleanProperty' => true];
}
$this->_checkPersistenceStatus($sourceEntity, $targetEntities);
$property = $this->property();
$this->junction()->connection()->transactional(function () use($sourceEntity, $targetEntities, $options) {
$links = $this->_collectJointEntities($sourceEntity, $targetEntities);
foreach ($links as $entity) {
$this->_junctionTable->delete($entity, $options);
}
});
$existing = $sourceEntity->get($property) ?: [];
if (!$options['cleanProperty'] || empty($existing)) {
return true;
}
$storage = new SplObjectStorage();
foreach ($targetEntities as $e) {
$storage->attach($e);
}
foreach ($existing as $k => $e) {
if ($storage->contains($e)) {
unset($existing[$k]);
}
}
$sourceEntity->set($property, array_values($existing));
$sourceEntity->dirty($property, false);
return true;
}