1
0
mirror of synced 2025-02-09 08:49:26 +03:00

Include example of a cascaded "persist" operation

Following up on https://github.com/doctrine/doctrine2/issues/2943 I started to clarify how it's supposed to be done.

Please check if this would be necessary (at line 511):
`$myFirstComment->setUser($user);`
...and add it (in case).
This commit is contained in:
Thomas Landauer 2016-12-14 00:10:39 +01:00 committed by GitHub
parent 6e6be3fdd9
commit 6aa81d1d36

View File

@ -453,11 +453,11 @@ following code:
$user->addComment($myFirstComment);
$em->persist($user);
$em->persist($myFirstComment);
$em->persist($myFirstComment); // mandatory if `persist` isn't set
$em->flush();
Even if you *persist* a new User that contains our new Comment this
code would fail if you removed the call to
code requires the explicit call to
``EntityManager#persist($myFirstComment)``. Doctrine 2 does not
cascade the persist operation to all nested entities that are new
as well.
@ -500,6 +500,18 @@ and the "remove" operation.
//...
}
Now you can persist the `User` entity like that:
.. code-block:: php
<?php
$user = new User();
$myFirstComment = new Comment();
$user->addComment($myFirstComment);
$em->persist($user);
$em->flush();
Even though automatic cascading is convenient it should be used
with care. Do not blindly apply cascade=all to all associations as
it will unnecessarily degrade the performance of your application.