How to use the cascade option in Doctrine2 to have associated entities automatically persisted?

后端 未结 1 1311
有刺的猬
有刺的猬 2021-01-12 01:16

Can someone explain me this:

$user = new User();

/* why do I have to call Entity Comment while trying to insert into db?  */
$myFirstComment = new Comment()         


        
相关标签:
1条回答
  • 2021-01-12 02:00

    To have Doctrine automatically handle the persistence of your User#comments property you have to set cascade to the "persist" operation.

    The cascade ( persist, remove , merge, all ) option gives you the ability to ommit ...

    $em->persist($myFirstComment);
    

    ... if you set it correctly on your inverse side of a bidirectional relation for example. It can also automatically remove User#comments if you remove a User entity with cascade "remove" !

    example:

    /**
     * Bidirectional - One-To-Many (INVERSE SIDE)
     *
     * @OneToMany(targetEntity="Comment", mappedBy="author", cascade={"persist", "remove"})
     */
    private $comments;
    

    Read more on Association mapping and cascade in the Transistive Persistence / Cascade Options chapter of the documentation.

    Please remember:

    Doctrine will only check the owning side of an association for changes.

    Changes made only to the inverse side of an association are ignored. Make sure to update both sides of a bidirectional association (or at least the owning side, from Doctrine’s point of view)

    • OneToMany associations are never the owning side.
    • The inverse side has to use the mappedBy attribute of the OneToOne, OneToMany, or ManyToMany mapping declaration. The mappedBy attribute contains the name of the association-field on the owning side
    • The owning side has to use the inversedBy attribute of the OneToOne, ManyToOne, or ManyToMany mapping declaration. The inversedBy attribute contains the name of the association-field on the inverse-side.
    • ManyToOne is always the owning side of a bidirectional association.
    • OneToMany is always the inverse side of a bidirectional association.

    Furthermore:

    you only have to call persist if you create a new root entity ( i.e. $user = new User() ) which is not already managed by doctrine ( and you don't have to call persist on $myFirstComment in your example if you have set the cascade option correctly ).

    Otherwise you only have to call flush if the entity hasn't for some reason been detached.

    0 讨论(0)
提交回复
热议问题