Doctrine2级联默认值

时间:2017-07-29 00:02:16

标签: symfony doctrine-orm symfony-3.3

我实际上正在学习Symfony3,更准确地说是对象之间的Doctrine2关系,我想知道当你没有明确表示cascade参数时是否有默认值。

我在教程中看到有必要使用未指定参数的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 个答案:

答案 0 :(得分:1)

简而言之,这两个片段不一样。如果您想通过FK删除与他人有关系的特定实体,则需要明确remove()相关实体以避免完整性约束违规。

每个

的例子

未定义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

相关问题