4   UnitOfWork

4.1   About the unitofwork

The UnitOfWork’s role is to track object changes to persist it into database.

4.1.1   Internal process

The UnitOfWork initializes in all objects “managed” a property tingUUID which must not in any way be altered by the user.

Un objet “managed” est un objet qui existe déjà en base et qui provoquera une requête UPDATE lorsque l’on demandera à ce qu’il soit persisté.

4.2   Principle

We ask the UnitOfWork to persist the data. It can be an update, or a deletion.

The orders are generally stacked in the queue of UnitOfWork then you ask it to treat the whole in a single process.

$unitOfWork->pushSave($user1);
$unitOfWork->pushSave($user2);
$unitOfWork->pushDelete($user3);
$unitOfWork->process();

4.2.1   Specifity

Whether it’s to persist updating or inserting an entity you must use pushSave.

Upon insertion of an entity which has an autoincrement property (configured in metadata) the autoincrement issued by the database will be set in the entity.

4.3   Notes for batch process

When processing a large number of items it is imperative to detach items that we don’t use anymore to free memory:

$unitOfWork->detach($entity);

We are also able to detach all objects when they have been persisted:

$unitOfWork->detachAll();