2017-07-29 119 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