From 22ac3a3099069432575962540810e1f8982b82b5 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sat, 17 Dec 2011 16:28:31 +0100 Subject: [PATCH] Add cookbook entry on saving entities in the session. --- en/cookbook/entities-in-session.rst | 68 +++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 en/cookbook/entities-in-session.rst diff --git a/en/cookbook/entities-in-session.rst b/en/cookbook/entities-in-session.rst new file mode 100644 index 000000000..48ea12249 --- /dev/null +++ b/en/cookbook/entities-in-session.rst @@ -0,0 +1,68 @@ +Entities in the Session +======================= + +There are several use-cases to save entities in the session, for example: + +1. User object +2. Multi-step forms + +To achieve this with Doctrine you have to pay attention to some details to get +this working. + +Merging entity into an EntityManager +------------------------------------ + +In Doctrine an entity objects has to be "managed" by an EntityManager to be +updateable. Entities saved into the session are not managed in the next request +anymore. This means that you have to register these entities with an +EntityManager again if you want to change them or use them as part of +references between other entities. You can achieve this by calling +``EntityManager#merge()``. + +For a representative User object the code to get turn an instance from +the session into a managed Doctrine object looks like this: + +.. code-block:: php + + merge($user); + } + +.. note:: + + A frequent mistake is not to get the merged user object from the return + value of ``EntityManager#merge()``. The entity object passed to merge is + not necessarily the same object that is returned from the method. + +Serializing entity into the session +----------------------------------- + +Entities that are serialized into the session normally contain references to +other entities as well. Think of the user entity has a reference to his +articles, groups, photos or many other different entities. If you serialize +this object into the session then you don't want to serialize the related +entities aswell. This is why you should call ``EntityManager#detach()`` on this +object or implement the __sleep() magic method on your entity. + +.. code-block:: php + + find("User", 1); + $em->detach($user); + $_SESSION['user'] = $user; + +.. note:: + + When you called detach on your objects they get "unmanaged" with that + entity manager. This means you cannot use them as part of write operations + during ``EntityManagr#flush()`` anymore in this request. +