the ondelete=cascade and cascad={ "remove"}

well, write it down for remind.
example:

class A{
    /**
     *
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="Jpo", mappedBy="institute", cascade={"persist","remove"},orphanRemoval=true)
     * @ORM\OrderBy({"id" = "ASC"})
     */
    protected $jpos;
}
This means if you delete a A, like $em->delete(a).It will delete all his jpos. no matter what you set in the jpo side.
class Jpo{
    /**
     * @var ArrayCollection
     *
     * @ORM\ManyToOne(targetEntity="A",inversedBy="jpos")
     * @ORM\JoinColumn(name="a_id", referencedColumnName="id",onDelete="CASCADE",nullable=false)
     * @Assert\NotNull()
     */
    protected $a;
}
this means when you do $em->delete(a), this will delete all his jpos. by the engine of Mysql (innoDB)
well, if you do this :
class Jpo{
    /**
     * @var ArrayCollection
     *
     * @ORM\ManyToOne(targetEntity="A",inversedBy="jpos",cascade={"remove"})
     * @ORM\JoinColumn(name="a_id", referencedColumnName="id",onDelete="CASCADE",nullable=false)
     * @Assert\NotNull()
     */
    protected $a;
}
you are fucked. Cause when you do $em->delete($jpo), Doctrine will try to call $em->delete($jpo->a), and this will call to delete all the jpos which are included by $a->jpos, and these jpos will ask to $em->delete($jpo->a->jpos[0-n]->a) and this is infinitive.

评论

热门博文