學說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]);
}
}