2011-07-15 59 views
1

Q有點懷疑,請原諒。如何使用Doctrine2關聯將M:1級聯設置爲空?

我想刪除的歌曲enitity:

我的1:M協會工作正常級聯=所有。例如。與歌曲相關的評分可以被刪除。

我的M:1我不知道該怎麼做。目前我將這些屬性設置爲null,然後堅持這些屬性,然後刪除歌曲。例如,專輯和藝術家應該留下,因爲它與其他歌曲相關聯。

摘錄:

/** 
* OWNING SIDE 
* @var My\Entity\Album 
* @ManyToOne(targetEntity="Album", inversedBy="songs") 
*/ 
private $album; 

/** 
* INVERSED SIDE 
* @var Doctrine\Common\Collections\ArrayCollection 
* @OneToMany(targetEntity="Similar", mappedBy="songa", cascade={"all"}) 
* @OrderBy({"id" = "DESC"}) 
*/ 
private $similarsa; 

我想使用級聯協會保持,而不是在數據庫級別。 任何關於使用$ em-> remove($ song)沒有額外的持久化我的M:1爲空值的建議?

回答

1

我發現它有以下幾點:

在我的歌(父)的實體:

/** @PreRemove */ 
public function preRemove() 
{ 
    $em = \Zend_Registry::get('doctrine')->getEntityManager(); 
    $em->getRepository('My\Entity\Similar')->removeSong($this); 
    $em->getRepository('My\Entity\Rating')->removeSong($this); 
} 

這些方法(在庫)的樣子:

/** 
* Removes similars with song 
*/ 
public function removeSong($song) 
{ 
    $ratings = $this->getSong($song); 
    foreach ($ratings as $rating) $this->getEntityManager()->remove($rating); 
    $this->getEntityManager()->flush(); 
} 

在我類似和評分等級:

/** @PreRemove */ 
public function preRemove() 
{ 
    $this->winner = $this->loser = null; 
}