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

后端 未结 4 1296
忘了有多久
忘了有多久 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 15:38

    Use onDelete="CASCADE" if you are using annotation

    /**
     * @ORM\ManyToOne(targetEntity="Report", inversedBy="responses")
     * @ORM\JoinColumn(name="reportId", referencedColumnName="id",onDelete="CASCADE")
     */
    

    Use onDelete: CASCADE if you are using yml

    joinColumn:
          name: pid
          referencedColumnName: id
          onDelete: CASCADE
    
    0 讨论(0)
  • 2020-12-05 15:48

    onDelete="CASCADE" also works fine. But don't forget to run app/console doctrine:schema:update --force before DB level changes will take an effect.

    0 讨论(0)
  • 2020-12-05 15:54

    orphanRemoval some times doesn't work because it depends on (gets schedulled in) PersistencCollection. And we might be calling ArrayCollection#removeElement().

    Following is a snippet of the PersistencCollection#remove()

        if ($this->association !== null &&
            $this->association['type'] & ClassMetadata::TO_MANY &&
            $this->owner &&
            $this->association['orphanRemoval']) {
            $this->em->getUnitOfWork()->scheduleOrphanRemoval($removed);
        }
    

    and ArrayCollection does not do that.

    0 讨论(0)
  • 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;
    
    0 讨论(0)
提交回复
热议问题