UnitOfWork
public class UnitOfWork
www.doctrine-project.org
Field Summary | |
---|---|
final int | A detached entity is an instance with a persistent identity that is not (or no longer) associated with an EntityManager (and a UnitOfWork). |
final int | An entity is in MANAGED state when its persistence is managed by an EntityManager. |
final int | An entity is new if it has just been instantiated (i.e. |
final int | A removed entity instance is an instance with a persistent identity, associated with an EntityManager, whose persistent state has been deleted (or is scheduled for deletion). |
Constructor Summary | |
---|---|
UnitOfWork(Doctrine\ORM\EntityManager em) Initializes a new UnitOfWork instance, bound to the given EntityManager. |
Method Summary | |
---|---|
boolean | addToIdentityMap(object entity) INTERNAL: Registers an entity in the identity map. |
void | clear() Clears the UnitOfWork. |
void | clearEntityChangeSet(string oid) INTERNAL: Clears the property changeset of the entity with the given OID. |
void | commit() Commits the UnitOfWork, executing all operations that have been postponed up to this point. |
void | computeChangeSet(ClassMetadata class, object entity) Computes the changes that happened to a single entity. |
void | Computes all the changes that have been done to entities and collections since the last commit and stores these changes in the _entityChangeSet map temporarily for access by the persisters, until the UoW commit is finished. |
boolean | containsIdHash(string idHash, string rootClassName) INTERNAL: Checks whether an identifier hash exists in the identity map. |
object | createEntity(string className, array data, array hints) INTERNAL: Creates an entity. |
void | detach(object entity) Detaches an entity from the persistence management. |
object | getByIdHash(string idHash, string rootClassName) INTERNAL: Gets an entity in the identity map by its identifier hash. |
AbstractCollectionPersister | getCollectionPersister(AssociationMapping association) Gets a collection persister for a collection-valued association. |
Doctrine\ORM\Internal\CommitOrderCalculator | Gets the CommitOrderCalculator used by the UnitOfWork to order commits. |
array | getEntityChangeSet(mixed entity) Gets the changeset for an entity. |
array | getEntityIdentifier(object entity) Gets the identifier of an entity. |
Doctrine\ORM\Persister\AbstractEntityPersister | getEntityPersister(string entityName) Gets the EntityPersister for an Entity. |
int | getEntityState(object entity, integer assume) Gets the state of an entity within the current unit of work. |
array | Gets the identity map of the UnitOfWork. |
array | getOriginalEntityData(object entity) Gets the original data of an entity. |
array | getScheduledCollectionDeletions() Get the currently scheduled complete collection deletions |
array | getScheduledCollectionUpdates() Gets the currently scheduled collection inserts, updates and deletes. |
array | Gets the currently scheduled entity deletions in this UnitOfWork. |
array | getScheduledEntityInsertions() Gets the currently scheduled entity insertions in this UnitOfWork. |
array | Gets the currently scheduled entity updates in this UnitOfWork. |
boolean | Checks whether the UnitOfWork has any pending insertions. |
void | isCollectionScheduledForDeletion(mixed coll) |
boolean | isEntityScheduled(mixed entity, $entity ) Checks whether an entity is scheduled for insertion, update or deletion. |
boolean | isInIdentityMap(object entity) Checks whether an entity is registered in the identity map of this UnitOfWork. |
boolean | isScheduledForDelete(object entity) Checks whether an entity is registered as removed/deleted with the unit of work. |
boolean | isScheduledForInsert(object entity) Checks whether an entity is scheduled for insertion. |
boolean | isScheduledForUpdate(object entity) Checks whether an entity is registered as dirty in the unit of work. |
object | merge(object entity) Merges the state of the given detached entity into this UnitOfWork. |
void | persist(object entity) Persists an entity as part of the current unit of work. |
void | propertyChanged(object entity, string propertyName, mixed oldValue, mixed newValue) Notifies this UnitOfWork of a property change in an entity. |
void | recomputeSingleEntityChangeSet(ClassMetadata class, object entity) INTERNAL: Computes the changeset of an individual entity, independently of the computeChangeSets() routine that is used at the beginning of a UnitOfWork#commit(). |
void | refresh(object entity) Refreshes the state of the given entity from the database, overwriting any local, unpersisted changes. |
void | registerManaged(object entity, array id, array data) INTERNAL: Registers an entity as managed. |
void | remove(object entity) Deletes an entity as part of the current unit of work. |
boolean | removeFromIdentityMap(object entity) INTERNAL: Removes an entity from the identity map. |
void | scheduleCollectionDeletion(PersistentCollection coll) INTERNAL: Schedules a complete collection for removal when this UnitOfWork commits. |
void | scheduleExtraUpdate(object entity, array changeset) INTERNAL: Schedules an extra update that will be executed immediately after the regular entity updates within the currently running commit cycle. |
void | scheduleForDelete(object entity) INTERNAL: Schedules an entity for deletion. |
void | scheduleForDirtyCheck(object entity) Schedules an entity for dirty-checking at commit-time. |
void | scheduleForInsert(object entity) Schedules an entity for insertion into the database. |
void | scheduleForUpdate(object entity) Schedules an entity for being updated. |
void | scheduleOrphanRemoval(object entity) INTERNAL: Schedules an orphaned entity for removal. |
void | setOriginalEntityData(mixed entity, mixed data) |
void | setOriginalEntityProperty(string oid, string property, mixed value) INTERNAL: Sets a property value of the original data array of an entity. |
integer | size() Calculates the size of the UnitOfWork. |
mixed | tryGetById(mixed id, string rootClassName) Tries to find an entity with the given identifier in the identity map of this UnitOfWork. |
mixed | tryGetByIdHash(string idHash, string rootClassName) INTERNAL: Tries to get an entity by its identifier hash. |
public final int STATE_DETACHED = 3
A detached entity is an instance with a persistent identity that is not (or no longer) associated with an EntityManager (and a UnitOfWork).
public final int STATE_MANAGED = 1
An entity is in MANAGED state when its persistence is managed by an EntityManager.
public final int STATE_NEW = 2
An entity is new if it has just been instantiated (i.e. using the "new" operator) and is not (yet) managed by an EntityManager.
public final int STATE_REMOVED = 4
A removed entity instance is an instance with a persistent identity, associated with an EntityManager, whose persistent state has been deleted (or is scheduled for deletion).
public UnitOfWork(Doctrine\ORM\EntityManager em)
Initializes a new UnitOfWork instance, bound to the given EntityManager.
public boolean addToIdentityMap(object entity)
INTERNAL: Registers an entity in the identity map. Note that entities in a hierarchy are registered with the class name of the root entity.
public void clear()
Clears the UnitOfWork.
public void clearEntityChangeSet(string oid)
INTERNAL: Clears the property changeset of the entity with the given OID.
public void commit()
Commits the UnitOfWork, executing all operations that have been postponed up to this point. The state of all managed entities will be synchronized with the database.
The operations are executed in the following order:
1) All entity insertions 2) All entity updates 3) All collection deletions 4) All collection updates 5) All entity deletions
public void computeChangeSet(ClassMetadata class, object entity)
Computes the changes that happened to a single entity.
Modifies/populates the following properties:
_originalEntityData
If the entity is NEW or MANAGED but not yet fully persisted (only has an id)
then it was not fetched from the database and therefore we have no original
entity data yet. All of the current entity data is stored as the original entity data.
_entityChangeSets
The changes detected on all properties of the entity are stored there.
A change is a tuple array where the first entry is the old value and the second
entry is the new value of the property. Changesets are used by persisters
to INSERT/UPDATE the persistent entity state.
_entityUpdates
If the entity is already fully MANAGED (has been fetched from the database before)
and any changes to its properties are detected, then a reference to the entity is stored
there to mark it for an update.
_collectionDeletions
If a PersistentCollection has been de-referenced in a fully MANAGED entity,
then this collection is marked for deletion.
public void computeChangeSets()
Computes all the changes that have been done to entities and collections since the last commit and stores these changes in the _entityChangeSet map temporarily for access by the persisters, until the UoW commit is finished.
public boolean containsIdHash(string idHash, string rootClassName)
INTERNAL: Checks whether an identifier hash exists in the identity map.
public object createEntity(string className, array data, array hints)
INTERNAL: Creates an entity. Used for reconstitution of entities during hydration.
public void detach(object entity)
Detaches an entity from the persistence management. It's persistence will no longer be managed by Doctrine.
public object getByIdHash(string idHash, string rootClassName)
INTERNAL: Gets an entity in the identity map by its identifier hash.
public AbstractCollectionPersister getCollectionPersister(AssociationMapping association)
Gets a collection persister for a collection-valued association.
public Doctrine\ORM\Internal\CommitOrderCalculator getCommitOrderCalculator()
Gets the CommitOrderCalculator used by the UnitOfWork to order commits.
public array getEntityChangeSet(mixed entity)
Gets the changeset for an entity.
public array getEntityIdentifier(object entity)
Gets the identifier of an entity. The returned value is always an array of identifier values. If the entity has a composite identifier then the identifier values are in the same order as the identifier field names as returned by ClassMetadata#getIdentifierFieldNames().
public Doctrine\ORM\Persister\AbstractEntityPersister getEntityPersister(string entityName)
Gets the EntityPersister for an Entity.
public int getEntityState(object entity, integer assume)
Gets the state of an entity within the current unit of work.
NOTE: This method sees entities that are not MANAGED or REMOVED and have a populated identifier, whether it is generated or manually assigned, as DETACHED. This can be incorrect for manually assigned identifiers.
public array getIdentityMap()
Gets the identity map of the UnitOfWork.
public array getOriginalEntityData(object entity)
Gets the original data of an entity. The original data is the data that was present at the time the entity was reconstituted from the database.
public array getScheduledCollectionDeletions()
Get the currently scheduled complete collection deletions
public array getScheduledCollectionUpdates()
Gets the currently scheduled collection inserts, updates and deletes.
public array getScheduledEntityDeletions()
Gets the currently scheduled entity deletions in this UnitOfWork.
public array getScheduledEntityInsertions()
Gets the currently scheduled entity insertions in this UnitOfWork.
public array getScheduledEntityUpdates()
Gets the currently scheduled entity updates in this UnitOfWork.
public boolean hasPendingInsertions()
Checks whether the UnitOfWork has any pending insertions.
public void isCollectionScheduledForDeletion(mixed coll)
public boolean isEntityScheduled(mixed entity, $entity )
Checks whether an entity is scheduled for insertion, update or deletion.
public boolean isInIdentityMap(object entity)
Checks whether an entity is registered in the identity map of this UnitOfWork.
public boolean isScheduledForDelete(object entity)
Checks whether an entity is registered as removed/deleted with the unit of work.
public boolean isScheduledForInsert(object entity)
Checks whether an entity is scheduled for insertion.
public boolean isScheduledForUpdate(object entity)
Checks whether an entity is registered as dirty in the unit of work. Note: Is not very useful currently as dirty entities are only registered at commit time.
public object merge(object entity)
Merges the state of the given detached entity into this UnitOfWork.
public void persist(object entity)
Persists an entity as part of the current unit of work.
public void propertyChanged(object entity, string propertyName, mixed oldValue, mixed newValue)
Notifies this UnitOfWork of a property change in an entity.
public void recomputeSingleEntityChangeSet(ClassMetadata class, object entity)
INTERNAL: Computes the changeset of an individual entity, independently of the computeChangeSets() routine that is used at the beginning of a UnitOfWork#commit().
The passed entity must be a managed entity. If the entity already has a change set because this method is invoked during a commit cycle then the change sets are added. whereby changes detected in this method prevail.
public void refresh(object entity)
Refreshes the state of the given entity from the database, overwriting any local, unpersisted changes.
public void registerManaged(object entity, array id, array data)
INTERNAL: Registers an entity as managed.
public void remove(object entity)
Deletes an entity as part of the current unit of work.
public boolean removeFromIdentityMap(object entity)
INTERNAL: Removes an entity from the identity map. This effectively detaches the entity from the persistence management of Doctrine.
public void scheduleCollectionDeletion(PersistentCollection coll)
INTERNAL: Schedules a complete collection for removal when this UnitOfWork commits.
public void scheduleExtraUpdate(object entity, array changeset)
INTERNAL: Schedules an extra update that will be executed immediately after the regular entity updates within the currently running commit cycle.
Extra updates for entities are stored as (entity, changeset) tuples.
public void scheduleForDelete(object entity)
INTERNAL: Schedules an entity for deletion.
public void scheduleForDirtyCheck(object entity)
Schedules an entity for dirty-checking at commit-time.
public void scheduleForInsert(object entity)
Schedules an entity for insertion into the database. If the entity already has an identifier, it will be added to the identity map.
public void scheduleForUpdate(object entity)
Schedules an entity for being updated.
public void scheduleOrphanRemoval(object entity)
INTERNAL: Schedules an orphaned entity for removal. The remove() operation will be invoked on that entity at the beginning of the next commit of this UnitOfWork.
public void setOriginalEntityData(mixed entity, mixed data)
public void setOriginalEntityProperty(string oid, string property, mixed value)
INTERNAL: Sets a property value of the original data array of an entity.
public integer size()
Calculates the size of the UnitOfWork. The size of the UnitOfWork is the number of entities in the identity map.
public mixed tryGetById(mixed id, string rootClassName)
Tries to find an entity with the given identifier in the identity map of this UnitOfWork.
public mixed tryGetByIdHash(string idHash, string rootClassName)
INTERNAL: Tries to get an entity by its identifier hash. If no entity is found for the given hash, FALSE is returned.
The UnitOfWork is responsible for tracking changes to objects during an "object-level" transaction and for writing out changes to the database in the correct order.