2012-06-12 89 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]); 
    } 
}