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();
$user->addComment($myFirstComment);
$em->persist($user);
$em->persist($myFirstComment);
$em->flush();
Why do I have to call Entity Comment while trying to insert into db?
I have cascade for that.
- Does this mean that if I have 50 relation in User Entity with other entities I have to call each relation manually when trying to update/insert/delete?
- Why does cascade exist if I have to do all manually?
If I have to call all that relation manually it is kind of stupid to use Doctrine at all.
I do not get this. Any help is appreciated.
This is connected with this: doctrine 2, unable to insert to database when relation is present
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 ...
... 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:
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)
mappedBy
attribute of the OneToOne, OneToMany, or ManyToMany mapping declaration. ThemappedBy
attribute contains the name of the association-field on the owning sideinversedBy
attribute of the OneToOne, ManyToOne, or ManyToMany mapping declaration. TheinversedBy
attribute contains the name of the association-field on the inverse-side.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.