Doctrine


Doctrine\ORM\UnitOfWork
/Doctrine/ORM/UnitOfWork.php at line 46

Class UnitOfWork

UnitOfWork
All Implemented Interfaces:
PropertyChangedListener

public class UnitOfWork

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.

License:
http://www.opensource.org/licenses/lgpl-license.php LGPL
See Also:
www.doctrine-project.org
Since:
2.0
Version:
$Revision$
Author:
Benjamin Eberlei
Guilherme Blanco
Jonathan Wage
Roman Borschel
Internal:
This class contains performance-critical code.

Field Summary
final int

STATE_DETACHED

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

STATE_MANAGED

An entity is in MANAGED state when its persistence is managed by an EntityManager.

final int

STATE_NEW

An entity is new if it has just been instantiated (i.e.

final int

STATE_REMOVED

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

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.

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

getCommitOrderCalculator()

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

getIdentityMap()

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

getScheduledEntityDeletions()

Gets the currently scheduled entity deletions in this UnitOfWork.

array

getScheduledEntityInsertions()

Gets the currently scheduled entity insertions in this UnitOfWork.

array

getScheduledEntityUpdates()

Gets the currently scheduled entity updates in this UnitOfWork.

boolean

hasPendingInsertions()

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.

Field Detail

/Doctrine/ORM/UnitOfWork.php at line 63

STATE_DETACHED

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).


/Doctrine/ORM/UnitOfWork.php at line 51

STATE_MANAGED

public final int STATE_MANAGED = 1

An entity is in MANAGED state when its persistence is managed by an EntityManager.


/Doctrine/ORM/UnitOfWork.php at line 57

STATE_NEW

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.


/Doctrine/ORM/UnitOfWork.php at line 70

STATE_REMOVED

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).


Constructor Detail

/Doctrine/ORM/UnitOfWork.php at line 236

UnitOfWork

public UnitOfWork(Doctrine\ORM\EntityManager em)

Initializes a new UnitOfWork instance, bound to the given EntityManager.


Method Detail

/Doctrine/ORM/UnitOfWork.php at line 1009

addToIdentityMap

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.

Ignore.
Parameters:
entity - The entity to register.
Returns:
TRUE if the registration was successful, FALSE if the identity of the entity in question is already managed.

/Doctrine/ORM/UnitOfWork.php at line 1650

clear

public void clear()

Clears the UnitOfWork.


/Doctrine/ORM/UnitOfWork.php at line 2030

clearEntityChangeSet

public void clearEntityChangeSet(string oid)

INTERNAL: Clears the property changeset of the entity with the given OID.

Parameters:
oid - The entity's OID.

/Doctrine/ORM/UnitOfWork.php at line 257

commit

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


/Doctrine/ORM/UnitOfWork.php at line 404

computeChangeSet

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.

Parameters:
class - The class descriptor of the entity.
entity - The entity for which to compute the changes.

/Doctrine/ORM/UnitOfWork.php at line 508

computeChangeSets

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.


/Doctrine/ORM/UnitOfWork.php at line 1149

containsIdHash

public boolean containsIdHash(string idHash, string rootClassName)

INTERNAL: Checks whether an identifier hash exists in the identity map.

Ignore.

/Doctrine/ORM/UnitOfWork.php at line 1715

createEntity

public object createEntity(string className, array data, array hints)

INTERNAL: Creates an entity. Used for reconstitution of entities during hydration.

Ignore.
Parameters:
className - The name of the entity class.
data - The data for the entity.
hints - Any hints to account for during reconstitution/lookup of the entity.
Returns:
The entity instance.
Internal:
Highly performance-sensitive method.
Todo:
Rename: getOrCreateEntity

/Doctrine/ORM/UnitOfWork.php at line 1415

detach

public void detach(object entity)

Detaches an entity from the persistence management. It's persistence will no longer be managed by Doctrine.

Parameters:
entity - The entity to detach.

/Doctrine/ORM/UnitOfWork.php at line 1098

getByIdHash

public object getByIdHash(string idHash, string rootClassName)

INTERNAL: Gets an entity in the identity map by its identifier hash.

Ignore.

/Doctrine/ORM/UnitOfWork.php at line 1993

getCollectionPersister

public AbstractCollectionPersister getCollectionPersister(AssociationMapping association)

Gets a collection persister for a collection-valued association.


/Doctrine/ORM/UnitOfWork.php at line 1639

getCommitOrderCalculator

public Doctrine\ORM\Internal\CommitOrderCalculator getCommitOrderCalculator()

Gets the CommitOrderCalculator used by the UnitOfWork to order commits.


/Doctrine/ORM/UnitOfWork.php at line 367

getEntityChangeSet

public array getEntityChangeSet(mixed entity)

Gets the changeset for an entity.


/Doctrine/ORM/UnitOfWork.php at line 1904

getEntityIdentifier

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().

Returns:
The identifier values.

/Doctrine/ORM/UnitOfWork.php at line 1969

getEntityPersister

public Doctrine\ORM\Persister\AbstractEntityPersister getEntityPersister(string entityName)

Gets the EntityPersister for an Entity.

Parameters:
entityName - The name of the Entity.

/Doctrine/ORM/UnitOfWork.php at line 1040

getEntityState

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.

Parameters:
assume - The state to assume if the state is not yet known. This is usually used to avoid costly state lookups, in the worst case with a database lookup.
Returns:
The entity state.

/Doctrine/ORM/UnitOfWork.php at line 1852

getIdentityMap

public array getIdentityMap()

Gets the identity map of the UnitOfWork.


/Doctrine/ORM/UnitOfWork.php at line 1864

getOriginalEntityData

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.


/Doctrine/ORM/UnitOfWork.php at line 2102

getScheduledCollectionDeletions

public array getScheduledCollectionDeletions()

Get the currently scheduled complete collection deletions


/Doctrine/ORM/UnitOfWork.php at line 2112

getScheduledCollectionUpdates

public array getScheduledCollectionUpdates()

Gets the currently scheduled collection inserts, updates and deletes.


/Doctrine/ORM/UnitOfWork.php at line 2092

getScheduledEntityDeletions

public array getScheduledEntityDeletions()

Gets the currently scheduled entity deletions in this UnitOfWork.


/Doctrine/ORM/UnitOfWork.php at line 2072

getScheduledEntityInsertions

public array getScheduledEntityInsertions()

Gets the currently scheduled entity insertions in this UnitOfWork.


/Doctrine/ORM/UnitOfWork.php at line 2082

getScheduledEntityUpdates

public array getScheduledEntityUpdates()

Gets the currently scheduled entity updates in this UnitOfWork.


/Doctrine/ORM/UnitOfWork.php at line 1943

hasPendingInsertions

public boolean hasPendingInsertions()

Checks whether the UnitOfWork has any pending insertions.

Returns:
TRUE if this UnitOfWork has pending insertions, FALSE otherwise.

/Doctrine/ORM/UnitOfWork.php at line 1697

isCollectionScheduledForDeletion

public void isCollectionScheduledForDeletion(mixed coll)

/Doctrine/ORM/UnitOfWork.php at line 990

isEntityScheduled

public boolean isEntityScheduled(mixed entity, $entity )

Checks whether an entity is scheduled for insertion, update or deletion.


/Doctrine/ORM/UnitOfWork.php at line 1125

isInIdentityMap

public boolean isInIdentityMap(object entity)

Checks whether an entity is registered in the identity map of this UnitOfWork.


/Doctrine/ORM/UnitOfWork.php at line 979

isScheduledForDelete

public boolean isScheduledForDelete(object entity)

Checks whether an entity is registered as removed/deleted with the unit of work.


/Doctrine/ORM/UnitOfWork.php at line 880

isScheduledForInsert

public boolean isScheduledForInsert(object entity)

Checks whether an entity is scheduled for insertion.


/Doctrine/ORM/UnitOfWork.php at line 935

isScheduledForUpdate

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.


/Doctrine/ORM/UnitOfWork.php at line 1297

merge

public object merge(object entity)

Merges the state of the given detached entity into this UnitOfWork.

Returns:
The managed copy of the entity.

/Doctrine/ORM/UnitOfWork.php at line 1159

persist

public void persist(object entity)

Persists an entity as part of the current unit of work.

Parameters:
entity - The entity to persist.

/Doctrine/ORM/UnitOfWork.php at line 2045

propertyChanged

public void propertyChanged(object entity, string propertyName, mixed oldValue, mixed newValue)

Notifies this UnitOfWork of a property change in an entity.

Parameters:
entity - The entity that owns the property.
propertyName - The name of the property that changed.
oldValue - The old value of the property.
newValue - The new value of the property.

/Doctrine/ORM/UnitOfWork.php at line 628

recomputeSingleEntityChangeSet

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.

Ignore.
Parameters:
class - The class descriptor of the entity.
entity - The entity for which to (re)calculate the change set.
Throws:
If the passed entity is not MANAGED.

/Doctrine/ORM/UnitOfWork.php at line 1459

refresh

public void refresh(object entity)

Refreshes the state of the given entity from the database, overwriting any local, unpersisted changes.

Parameters:
entity - The entity to refresh.
Throws:
If the entity is not MANAGED.

/Doctrine/ORM/UnitOfWork.php at line 2015

registerManaged

public void registerManaged(object entity, array id, array data)

INTERNAL: Registers an entity as managed.

Parameters:
entity - The entity.
id - The identifier values.
data - The original entity data.

/Doctrine/ORM/UnitOfWork.php at line 1241

remove

public void remove(object entity)

Deletes an entity as part of the current unit of work.

Parameters:
entity - The entity to remove.

/Doctrine/ORM/UnitOfWork.php at line 1071

removeFromIdentityMap

public boolean removeFromIdentityMap(object entity)

INTERNAL: Removes an entity from the identity map. This effectively detaches the entity from the persistence management of Doctrine.

Ignore.

/Doctrine/ORM/UnitOfWork.php at line 1690

scheduleCollectionDeletion

public void scheduleCollectionDeletion(PersistentCollection coll)

INTERNAL: Schedules a complete collection for removal when this UnitOfWork commits.


/Doctrine/ORM/UnitOfWork.php at line 916

scheduleExtraUpdate

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.

Ignore.
Parameters:
entity - The entity for which to schedule an extra update.
changeset - The changeset of the entity (what to update).

/Doctrine/ORM/UnitOfWork.php at line 946

scheduleForDelete

public void scheduleForDelete(object entity)

INTERNAL: Schedules an entity for deletion.


/Doctrine/ORM/UnitOfWork.php at line 1932

scheduleForDirtyCheck

public void scheduleForDirtyCheck(object entity)

Schedules an entity for dirty-checking at commit-time.

Parameters:
entity - The entity to schedule for dirty-checking.

/Doctrine/ORM/UnitOfWork.php at line 853

scheduleForInsert

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.

Parameters:
entity - The entity to schedule for insertion.

/Doctrine/ORM/UnitOfWork.php at line 890

scheduleForUpdate

public void scheduleForUpdate(object entity)

Schedules an entity for being updated.

Parameters:
entity - The entity to schedule for being updated.

/Doctrine/ORM/UnitOfWork.php at line 1679

scheduleOrphanRemoval

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.

Ignore.

/Doctrine/ORM/UnitOfWork.php at line 1876

setOriginalEntityData

public void setOriginalEntityData(mixed entity, mixed data)

Ignore.

/Doctrine/ORM/UnitOfWork.php at line 1890

setOriginalEntityProperty

public void setOriginalEntityProperty(string oid, string property, mixed value)

INTERNAL: Sets a property value of the original data array of an entity.

Ignore.

/Doctrine/ORM/UnitOfWork.php at line 1954

size

public integer size()

Calculates the size of the UnitOfWork. The size of the UnitOfWork is the number of entities in the identity map.


/Doctrine/ORM/UnitOfWork.php at line 1918

tryGetById

public mixed tryGetById(mixed id, string rootClassName)

Tries to find an entity with the given identifier in the identity map of this UnitOfWork.

Parameters:
id - The entity identifier to look for.
rootClassName - The name of the root class of the mapped entity hierarchy.
Returns:
Returns the entity with the specified identifier if it exists in this UnitOfWork, FALSE otherwise.

/Doctrine/ORM/UnitOfWork.php at line 1113

tryGetByIdHash

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.

Ignore.
Returns:
The found entity or FALSE.

Doctrine