(doctrine2 + symfony2) cascading remove : integrity constraint violation 1451

后端 未结 4 1310
忘了有多久
忘了有多久 2020-12-05 15:01

First, sorry for my poor English...

I got four entities : User, Application, Bundle & Entity. Here are their relations (with cascading persist & remove, see

4条回答
  •  抹茶落季
    2020-12-05 16:05

    So, thanks to this French forum, I fixed the problem.

    I needed to add nullable=true & onDelete="SET NULL" in @ORM\JoinColumn

    Here is the workable configuration, maybe it will help someone :

    #User.
        /**
         * @ORM\OneToMany(targetEntity="\sfCommands\ContentBundle\Entity\Application", mappedBy="user", cascade={"remove"}, orphanRemoval=true)
         * @ORM\OrderBy({"name" = "ASC"})
         */
        protected $applications;
    
        /**
         * @ORM\OneToOne(targetEntity="\sfCommands\ContentBundle\Entity\Entity")
         * @ORM\JoinColumn(name="entity1_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
         */
        private $entity1;
    
        /**
         * @ORM\OneToOne(targetEntity="\sfCommands\ContentBundle\Entity\Entity")
         * @ORM\JoinColumn(name="entity2_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
         */
        private $entity2;
    
    #Application.
        /**
         * @ORM\OneToMany(targetEntity="\sfCommands\ContentBundle\Entity\Bundle", mappedBy="application", cascade={"remove"}, orphanRemoval=true)
         * @ORM\OrderBy({"name" = "ASC"})
         */
        protected $bundles;
    
        /**
         * @ORM\ManyToOne(targetEntity="\sfCommands\UserBundle\Entity\User", inversedBy="applications", cascade={"persist"})
         * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
         */
        protected $user;
    
    #Bundle.
        /**
         * @ORM\ManyToOne(targetEntity="\sfCommands\ContentBundle\Entity\Application", inversedBy="bundles", cascade={"persist"})
         * @ORM\JoinColumn(name="application_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
         */
        protected $application;
    
        /**
         * @ORM\OneToMany(targetEntity="\sfCommands\ContentBundle\Entity\Entity", mappedBy="bundle", cascade={"remove"}, orphanRemoval=true)
         * @ORM\OrderBy({"name" = "ASC"})
         */
        protected $entitys;
    
    #Entity.
        /**
         * @ORM\ManyToOne(targetEntity="\sfCommands\ContentBundle\Entity\Bundle", inversedBy="entitys", cascade={"persist"})
         * @ORM\JoinColumn(name="bundle_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
         */
        protected $bundle;
    

提交回复
热议问题