Merge pull request #6171 from ThomasLandauer/patch-3
Include example of a cascaded "persist" operation
This commit is contained in:
commit
ce4abdea55
@ -407,25 +407,11 @@ There are two approaches to handle this problem in your code:
|
|||||||
Transitive persistence / Cascade Operations
|
Transitive persistence / Cascade Operations
|
||||||
-------------------------------------------
|
-------------------------------------------
|
||||||
|
|
||||||
Persisting, removing, detaching, refreshing and merging individual entities can
|
When working with many associated entities, you sometimes don't want to "expose" all entities to your PHP application.
|
||||||
become pretty cumbersome, especially when a highly interweaved object graph
|
Therefore Doctrine 2 provides a mechanism for transitive persistence through cascading of certain operations.
|
||||||
is involved. Therefore Doctrine 2 provides a
|
Each association to another entity or a collection of
|
||||||
mechanism for transitive persistence through cascading of these
|
entities can be configured to automatically cascade the following operations to the associated entities:
|
||||||
operations. Each association to another entity or a collection of
|
``persist``, ``remove``, ``merge``, ``detach``, ``refresh`` or ``all``.
|
||||||
entities can be configured to automatically cascade certain
|
|
||||||
operations. By default, no operations are cascaded.
|
|
||||||
|
|
||||||
The following cascade options exist:
|
|
||||||
|
|
||||||
|
|
||||||
- persist : Cascades persist operations to the associated
|
|
||||||
entities.
|
|
||||||
- remove : Cascades remove operations to the associated entities.
|
|
||||||
- merge : Cascades merge operations to the associated entities.
|
|
||||||
- detach : Cascades detach operations to the associated entities.
|
|
||||||
- refresh : Cascades refresh operations to the associated entities.
|
|
||||||
- all : Cascades persist, remove, merge, refresh and detach operations to
|
|
||||||
associated entities.
|
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
@ -444,7 +430,7 @@ The following cascade options exist:
|
|||||||
|
|
||||||
The following example is an extension to the User-Comment example
|
The following example is an extension to the User-Comment example
|
||||||
of this chapter. Suppose in our application a user is created
|
of this chapter. Suppose in our application a user is created
|
||||||
whenever he writes his first comment. In this case we would use the
|
whenever they write their first comment. In this case we would use the
|
||||||
following code:
|
following code:
|
||||||
|
|
||||||
.. code-block:: php
|
.. code-block:: php
|
||||||
@ -455,16 +441,16 @@ following code:
|
|||||||
$user->addComment($myFirstComment);
|
$user->addComment($myFirstComment);
|
||||||
|
|
||||||
$em->persist($user);
|
$em->persist($user);
|
||||||
$em->persist($myFirstComment);
|
$em->persist($myFirstComment); // required, if `cascade: persist` isn't set
|
||||||
$em->flush();
|
$em->flush();
|
||||||
|
|
||||||
Even if you *persist* a new User that contains our new Comment this
|
Even if you *persist* a new User that contains our new Comment this
|
||||||
code would fail if you removed the call to
|
code requires an explicit call to
|
||||||
``EntityManager#persist($myFirstComment)``. Doctrine 2 does not
|
``EntityManager#persist($myFirstComment)``. Doctrine 2 does not
|
||||||
cascade the persist operation to all nested entities that are new
|
cascade the persist operation to all nested entities that are new
|
||||||
as well.
|
as well.
|
||||||
|
|
||||||
More complicated is the deletion of all of a user's comments when he is
|
More complicated is the deletion of all user's comments when the user is
|
||||||
removed from the system:
|
removed from the system:
|
||||||
|
|
||||||
.. code-block:: php
|
.. code-block:: php
|
||||||
@ -502,8 +488,28 @@ and the "remove" operation.
|
|||||||
//...
|
//...
|
||||||
}
|
}
|
||||||
|
|
||||||
Even though automatic cascading is convenient it should be used
|
Since ``cascade: persist`` is configured for the ``User#commentsAuthored``
|
||||||
with care. Do not blindly apply cascade=all to all associations as
|
association, you can now create a user and persist their comments as follows:
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
<?php
|
||||||
|
$user = new User();
|
||||||
|
$myFirstComment = new Comment();
|
||||||
|
$user->addComment($myFirstComment);
|
||||||
|
$myFirstComment->setUser($user);
|
||||||
|
|
||||||
|
$em->persist($user);
|
||||||
|
$em->flush();
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
This code does not associate the newly created comment with the user.
|
||||||
|
To achieve this, you *always* have to call ``$myFirstComment->setUser($user);`` –
|
||||||
|
no matter if ``cascade: persist`` is set or not.
|
||||||
|
|
||||||
|
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.
|
it will unnecessarily degrade the performance of your application.
|
||||||
For each cascade operation that gets activated Doctrine also
|
For each cascade operation that gets activated Doctrine also
|
||||||
applies that operation to the association, be it single or
|
applies that operation to the association, be it single or
|
||||||
|
Loading…
x
Reference in New Issue
Block a user