简体   繁体   English

关于删除级联学说

[英]On delete cascade Doctrine

I have two entities that i want to link: User and Folder 我有两个要链接的实体: UserFolder

What i want to do is: 我想做的是:

  • Each folder has aa parent folder (self-referencing)but it is possible to don't have one (null allowed). 每个文件夹都有一个父文件夹(自引用),但可能没有一个(允许为空)。

  • Each user has a default folder (one to one i think) witch doesn't have a parent folder. 每个用户都有一个默认文件夹(我认为一对一),女巫没有父文件夹。

  • When i delete a user, i want that MySQL automatically delete default folder and subfolders. 当我删除用户时,我希望MySQL自动删除默认文件夹和子文件夹。

The problem I have is i can't delete a User beceause I have a FK. 我的问题是我无法删除用户,因为我有FK。 ErrorMessage : 错误信息 :

#1451 - Cannot delete or update a parent row: a foreign key constraint fails (`supbox`.`folder`, CONSTRAINT `FK_EB0E0CFB7E3C61F9` FOREIGN KEY (`owner_id`) REFERENCES `user` (`id`)) 

My entities: 我的实体:

<?php   
class User // Supbox\UserBundle\Entity\User
{


    [...]

   /**
     * @var integer
     * 
     * @ORM\ManyToOne(targetEntity="Supbox\CloudBundle\Entity\Folder")
     */
    private $folder;
}


class Folder // Supbox\CloudBundle\Entity\Folder
{


    [...]

    /**
     * @ORM\OneToOne(targetEntity="Supbox\UserBundle\Entity\User" , cascade={"remove"})
     * @ORM\JoinColumn(nullable=false)
     */
    private $owner;

     /**
     * @var integer
     * 
     * @ORM\ManyToOne(targetEntity="Supbox\CloudBundle\Entity\Folder")
     * @ORM\JoinColumn(nullable=true)
     */
    private $parent = null;
}

 ?>

您需要在用户实体的$folder字段中添加cascade={"remove"}部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM