2012-06-12 91 views
0

學說2文檔指出:刪除使用相關實體ID關聯的學說2

刪除兩個實體之間的關聯同樣 直線前進。有兩種策略可以做到這一點,通過鍵和 元素。

「按鍵」的含義是什麼?它是相關實體的id字段,還是集合中相關實體的位置?例如這裏$ithComment被使用(即註釋的位置):

// Remove by Key 
$user->getComments()->remove($ithComment); 
$comment->setAuthor(null); 

回答

1

它是集合中相關實體的位置。在檢查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; 
} 

您可以看到沒有引用收集項目標識符被使用。

0

我已經解決,也許是對你有好處:

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