2017-07-29 113 views
0

我實際上在學習Symfony3,更確切地說是對象之間的Doctrine2關係,我想知道在不解釋級聯參數時是否有默認值。Doctrine2級聯默認值

我在教程中看到有必要使用未指定參數的remove值,但沒有解釋這個事實。

所以,我的意思是這

/** 
* @ORM\ManyToOne(targetEntity="UTM\ForumBundle\Entity\UtmWebsiteTopics") 
* @ORM\JoinColumn(nullable=false) 
*/ 
private $topic; 

相當於?

/** 
* @ORM\ManyToOne(targetEntity="UTM\ForumBundle\Entity\UtmWebsiteTopics", cascade={"remove"}) 
* @ORM\JoinColumn(nullable=false) 
*/ 
private $topic; 

謝謝你的閱讀,我希望你能給我一個答案。 :D

+1

假設'cascade'沒有默認值。 –

回答

0

總之,這兩個片段是不一樣的。如果您想要通過FK刪除與其他人有關係的特定實體,則需要明確指出相關實體以避免完整性約束違規。

的每個

實例並非限定cascade={"remove"}

public function removeEntityAction($id) 
{ 
    // Get entity manager etc.... 
    $myEntity = $em->getRepository("MyEntity")->findBy(["id" => $id]); 

    foreach($myEntity->getTopics() as $topic) { 
     $em->remove($topic); 
    } 

    $em->remove($myEntity); 
} 

定義cascade={"remove"}

public function removeEntityAction($id) 
{ 
    // Get entity manager etc.... 
    $myEntity = $em->getRepository("MyEntity")->findBy(["id" => $id]); 

    $em->remove($myEntity); 
} 

Doctrine Cascade Operations

Doctrine - Removing Entities