学说2文档指出:删除使用相关实体ID关联的学说2
删除两个实体之间的关联同样 直线前进。有两种策略可以做到这一点,通过键和 元素。
“按键”的含义是什么?它是相关实体的id
字段,还是集合中相关实体的位置?例如这里$ithComment
被使用(即注释的位置):
// Remove by Key
$user->getComments()->remove($ithComment);
$comment->setAuthor(null);
学说2文档指出:删除使用相关实体ID关联的学说2
删除两个实体之间的关联同样 直线前进。有两种策略可以做到这一点,通过键和 元素。
“按键”的含义是什么?它是相关实体的id
字段,还是集合中相关实体的位置?例如这里$ithComment
被使用(即注释的位置):
// Remove by Key
$user->getComments()->remove($ithComment);
$comment->setAuthor(null);
它是集合中相关实体的位置。在检查ArrayCollection ..
public function add($value)
{
$this->_elements[] = $value;
return true;
}
public function remove($key)
{
if (isset($this->_elements[$key])) {
$removed = $this->_elements[$key];
unset($this->_elements[$key]);
return $removed;
}
return null;
}
您可以看到没有引用收集项目标识符被使用。
根据Doctrine2 API,一个学说集合的remove方法,
“指定索引处的元素从集合”
(见https://github.com/doctrine/common/blob/master/lib/Doctrine/Common/Collections/Collection.php#L78)
我的结论是,你谈论的关键确实是相关实体的位置。
我已经解决,也许是对你有好处:
public function addSectors(ArrayCollection $sectors)
{
foreach($sectors as $k => $sector) {
$this->addSector($sector);
}
}
public function removeSectors($sectors)
{
foreach($sectors as $k => $sector) {
unset($this->sectors[$k]);
}
}