Take the 2-minute tour ×

The question is about Doctirne but I think that can be extended to many ORM's.


An entity is detached from an EntityManager and thus no longer managed by invoking the EntityManager#detach($entity) method on it or by cascading the detach operation to it. Changes made to the detached entity, if any (including removal of the entity), will not be synchronized to the database after the entity has been detached.


Merging entities refers to the merging of (usually detached) entities into the context of an EntityManager so that they become managed again. To merge the state of an entity into an EntityManager use the EntityManager#merge($entity) method. The state of the passed entity will be merged into a managed copy of this entity and this copy will subsequently be returned.

I understand (almost) how this works, but the question is: why one would need detaching/merging entitiies? Can you give me an example/scenario when these two operations can be used/needed?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

When should I Detaching an entity?
Detaching an entity from the an EM (EntityManager) is widely used when you deal with more than one EM and avoid concurrency conflicts, for example:

$user= $em->find('models\User', 1);

// You can not remove this user, 
// because it still attached to the first Entity Manager

You can not take control of $user object by $em2 because its session belongs to $em that inicially load the $user from database. Them how to solve the problem above? You need to detaching the object:



When should I use merging function?
Basically when you want to update an entity:



The EM will make a compare between the $user in database vs the $user in memory. Once the EM recognize the changed fields, it only updates them and keeps the old ones.

The flush method triggers a commit and the user name will updated in the database

share|improve this answer
I agree with all said except with what is said about flush. The flush method synchronize the entities with the database but inside the transaction boundaries. This comes to play upon invoking a query within the very same transaction. Thus you quarantee that the processing of the query takes into account this very transaction updates. –  Sam Nov 19 '12 at 20:55
You are correct @Sam, I was wrong with flush method. –  manix Nov 21 '12 at 14:18
This is a good explanation (+1), but why one should use more than one entity manager in the first place? Can't get it, sorry, using Doctrine always with one entity manager... –  gremo Nov 26 '12 at 21:56
@Gremo, this could happen when you need are dealing with more than one datababase, so an EM per dabatase is required. At least this is one scenario –  manix Nov 27 '12 at 14:22

You would need to detach an entity when dealing with issues of concurrency.

Suppose you are using an asynchronous API that makes callbacks to your project. When you issue the API call along with callback instruction, you might still be managing the entity that is affected by the callback, and therefore overwrite the changes made by the callback.

share|improve this answer

Your Answer


