class EntityManager implements EntityManagerInterface (View source)

Methods

__construct(Connection $conn, Configuration $config, EventManager $eventManager)

Creates a new EntityManager that operates on the given database connection and uses the given Configuration and EventManager implementations.

Connection
getConnection()

Gets the database connection object used by the EntityManager.

getMetadataFactory()

Gets the metadata factory used to gather the metadata of classes.

getExpressionBuilder()

Gets an ExpressionBuilder used for object-oriented construction of query expressions.

void
beginTransaction()

Starts a transaction on the underlying database connection.

mixed
transactional(callable $func)

Executes a function in a transaction.

void
commit()

Commits a transaction on the underlying database connection.

void
rollback()

Performs a rollback on the underlying database connection.

getClassMetadata(string $className)

Returns the ORM metadata descriptor for a class.

createQuery(string $dql = '')

Creates a new Query object.

createNamedQuery(string $name)

Creates a Query from a named query.

createNativeQuery(string $sql, ResultSetMapping $rsm)

Creates a native SQL query.

createNamedNativeQuery(string $name)

Creates a NativeQuery from a named native query.

createQueryBuilder()

Create a QueryBuilder instance

void
flush(null|object|array $entity = null)

Flushes all changes to objects that have been queued up to now to the database.

object|null
find(string $entityName, mixed $id, integer $lockMode = LockMode::NONE, integer|null $lockVersion = null)

Finds an Entity by its identifier.

object
getReference(string $entityName, mixed $id)

Gets a reference to the entity identified by the given type and identifier without actually loading it, if the entity is not yet loaded.

object
getPartialReference(string $entityName, mixed $identifier)

Gets a partial reference to the entity identified by the given type and identifier without actually loading it, if the entity is not yet loaded.

void
clear(string|null $entityName = null)

Clears the EntityManager. All entities that are currently managed by this EntityManager become detached.

void
close()

Closes the EntityManager. All entities that are currently managed by this EntityManager become detached. The EntityManager may no longer be used after it is closed.

void
persist(object $entity)

Tells the EntityManager to make an instance managed and persistent.

void
remove(object $entity)

Removes an entity instance.

void
refresh(object $entity)

Refreshes the persistent state of an entity from the database, overriding any local changes that have not yet been persisted.

void
detach(object $entity)

Detaches an entity from the EntityManager, causing a managed entity to become detached. Unflushed changes made to the entity if any (including removal of the entity), will not be synchronized to the database.

object
merge(object $entity)

Merges the state of a detached entity into the persistence context of this EntityManager and returns the managed copy of the entity.

object
copy(object $entity, boolean $deep = false)

Creates a copy of the given entity. Can create a shallow or a deep copy.

void
lock(object $entity, int $lockMode, int|null $lockVersion = null)

Acquire a lock on the given entity.

getRepository(string $entityName)

Gets the repository for an entity class.

boolean
contains(object $entity)

Determines whether an entity instance is managed in this EntityManager.

EventManager
getEventManager()

Gets the EventManager used by the EntityManager.

getConfiguration()

Gets the Configuration used by the EntityManager.

bool
isOpen()

Check if the Entity manager is open or closed.

getUnitOfWork()

Gets the UnitOfWork used by the EntityManager to coordinate operations.

getHydrator(int $hydrationMode)

Gets a hydrator for the given hydration mode.

newHydrator(int $hydrationMode)

Create a new instance for the given hydration mode.

getProxyFactory()

Gets the proxy factory used by the EntityManager to create entity proxies.

initializeObject($obj)

{@inheritDoc}

static EntityManager
create(mixed $conn, Configuration $config, EventManager $eventManager = null)

Factory method to create EntityManager instances.

getFilters()

Gets the enabled filters.

boolean
isFiltersStateClean()

Checks whether the state of the filter collection is clean.

boolean
hasFilters()

Checks whether the Entity Manager has filters.

Details

protected __construct(Connection $conn, Configuration $config, EventManager $eventManager)

Creates a new EntityManager that operates on the given database connection and uses the given Configuration and EventManager implementations.

Parameters

Connection $conn
Configuration $config
EventManager $eventManager

Connection getConnection()

Gets the database connection object used by the EntityManager.

Return Value

Connection

ClassMetadataFactory getMetadataFactory()

Gets the metadata factory used to gather the metadata of classes.

Return Value

ClassMetadataFactory

Expr getExpressionBuilder()

Gets an ExpressionBuilder used for object-oriented construction of query expressions.

Example:

$qb = $em->createQueryBuilder(); $expr = $em->getExpressionBuilder(); $qb->select('u')->from('User', 'u') ->where($expr->orX($expr->eq('u.id', 1), $expr->eq('u.id', 2)));

Return Value

Expr

void beginTransaction()

Starts a transaction on the underlying database connection.

Return Value

void

mixed transactional(callable $func)

Executes a function in a transaction.

The function gets passed this EntityManager instance as an (optional) parameter.

{@link flush} is invoked prior to transaction commit.

If an exception occurs during execution of the function or flushing or transaction commit, the transaction is rolled back, the EntityManager closed and the exception re-thrown.

Parameters

callable $func The function to execute transactionally.

Return Value

mixed The non-empty value returned from the closure or true instead.

void commit()

Commits a transaction on the underlying database connection.

Return Value

void

void rollback()

Performs a rollback on the underlying database connection.

Return Value

void

ClassMetadata getClassMetadata(string $className)

Returns the ORM metadata descriptor for a class.

The class name must be the fully-qualified class name without a leading backslash (as it is returned by get_class($obj)) or an aliased class name.

Examples: MyProject\Domain\User sales:PriceRequest

Parameters

string $className

Return Value

ClassMetadata

Query createQuery(string $dql = '')

Creates a new Query object.

Parameters

string $dql The DQL string.

Return Value

Query

Query createNamedQuery(string $name)

Creates a Query from a named query.

Parameters

string $name

Return Value

Query

NativeQuery createNativeQuery(string $sql, ResultSetMapping $rsm)

Creates a native SQL query.

Parameters

string $sql
ResultSetMapping $rsm The ResultSetMapping to use.

Return Value

NativeQuery

NativeQuery createNamedNativeQuery(string $name)

Creates a NativeQuery from a named native query.

Parameters

string $name

Return Value

NativeQuery

QueryBuilder createQueryBuilder()

Create a QueryBuilder instance

Return Value

QueryBuilder

void flush(null|object|array $entity = null)

Flushes all changes to objects that have been queued up to now to the database.

This effectively synchronizes the in-memory state of managed objects with the database.

If an entity is explicitly passed to this method only this entity and the cascade-persist semantics + scheduled inserts/removals are synchronized.

Parameters

null|object|array $entity

Return Value

void

Exceptions

OptimisticLockException If a version check on an entity that makes use of optimistic locking fails.

object|null find(string $entityName, mixed $id, integer $lockMode = LockMode::NONE, integer|null $lockVersion = null)

Finds an Entity by its identifier.

Parameters

string $entityName
mixed $id
integer $lockMode
integer|null $lockVersion

Return Value

object|null The entity instance or NULL if the entity can not be found.

Exceptions

OptimisticLockException
ORMInvalidArgumentException
TransactionRequiredException
ORMException

object getReference(string $entityName, mixed $id)

Gets a reference to the entity identified by the given type and identifier without actually loading it, if the entity is not yet loaded.

Parameters

string $entityName The name of the entity type.
mixed $id The entity identifier.

Return Value

object The entity reference.

Exceptions

ORMException

object getPartialReference(string $entityName, mixed $identifier)

Gets a partial reference to the entity identified by the given type and identifier without actually loading it, if the entity is not yet loaded.

The returned reference may be a partial object if the entity is not yet loaded/managed. If it is a partial object it will not initialize the rest of the entity state on access. Thus you can only ever safely access the identifier of an entity obtained through this method.

The use-cases for partial references involve maintaining bidirectional associations without loading one side of the association or to update an entity without loading it. Note, however, that in the latter case the original (persistent) entity data will never be visible to the application (especially not event listeners) as it will never be loaded in the first place.

Parameters

string $entityName The name of the entity type.
mixed $identifier The entity identifier.

Return Value

object The (partial) entity reference.

void clear(string|null $entityName = null)

Clears the EntityManager. All entities that are currently managed by this EntityManager become detached.

Parameters

string|null $entityName if given, only entities of this type will get detached

Return Value

void

void close()

Closes the EntityManager. All entities that are currently managed by this EntityManager become detached. The EntityManager may no longer be used after it is closed.

Return Value

void

void persist(object $entity)

Tells the EntityManager to make an instance managed and persistent.

The entity will be entered into the database at or before transaction commit or as a result of the flush operation.

NOTE: The persist operation always considers entities that are not yet known to this EntityManager as NEW. Do not pass detached entities to the persist operation.

Parameters

object $entity The instance to make managed and persistent.

Return Value

void

Exceptions

ORMInvalidArgumentException

void remove(object $entity)

Removes an entity instance.

A removed entity will be removed from the database at or before transaction commit or as a result of the flush operation.

Parameters

object $entity The entity instance to remove.

Return Value

void

Exceptions

ORMInvalidArgumentException

void refresh(object $entity)

Refreshes the persistent state of an entity from the database, overriding any local changes that have not yet been persisted.

Parameters

object $entity The entity to refresh.

Return Value

void

Exceptions

ORMInvalidArgumentException

void detach(object $entity)

Detaches an entity from the EntityManager, causing a managed entity to become detached. Unflushed changes made to the entity if any (including removal of the entity), will not be synchronized to the database.

Entities which previously referenced the detached entity will continue to reference it.

Parameters

object $entity The entity to detach.

Return Value

void

Exceptions

ORMInvalidArgumentException

object merge(object $entity)

Merges the state of a detached entity into the persistence context of this EntityManager and returns the managed copy of the entity.

The entity passed to merge will not become associated/managed with this EntityManager.

Parameters

object $entity The detached entity to merge into the persistence context.

Return Value

object The managed copy of the entity.

Exceptions

ORMInvalidArgumentException

object copy(object $entity, boolean $deep = false)

Creates a copy of the given entity. Can create a shallow or a deep copy.

Parameters

object $entity The entity to copy.
boolean $deep FALSE for a shallow copy, TRUE for a deep copy.

Return Value

object The new entity.

Exceptions

BadMethodCallException

void lock(object $entity, int $lockMode, int|null $lockVersion = null)

Acquire a lock on the given entity.

Parameters

object $entity
int $lockMode
int|null $lockVersion

Return Value

void

Exceptions

OptimisticLockException
PessimisticLockException

EntityRepository getRepository(string $entityName)

Gets the repository for an entity class.

Parameters

string $entityName The name of the entity.

Return Value

EntityRepository The repository class.

boolean contains(object $entity)

Determines whether an entity instance is managed in this EntityManager.

Parameters

object $entity

Return Value

boolean TRUE if this EntityManager currently manages the given entity, FALSE otherwise.

EventManager getEventManager()

Gets the EventManager used by the EntityManager.

Return Value

EventManager

Configuration getConfiguration()

Gets the Configuration used by the EntityManager.

Return Value

Configuration

bool isOpen()

Check if the Entity manager is open or closed.

Return Value

bool

UnitOfWork getUnitOfWork()

Gets the UnitOfWork used by the EntityManager to coordinate operations.

Return Value

UnitOfWork

AbstractHydrator getHydrator(int $hydrationMode)

Gets a hydrator for the given hydration mode.

This method caches the hydrator instances which is used for all queries that don't selectively iterate over the result.

Parameters

int $hydrationMode

Return Value

AbstractHydrator

AbstractHydrator newHydrator(int $hydrationMode)

Create a new instance for the given hydration mode.

Parameters

int $hydrationMode

Return Value

AbstractHydrator

Exceptions

ORMException

ProxyFactory getProxyFactory()

Gets the proxy factory used by the EntityManager to create entity proxies.

Return Value

ProxyFactory

initializeObject($obj)

{@inheritDoc}

Parameters

$obj

static EntityManager create(mixed $conn, Configuration $config, EventManager $eventManager = null)

Factory method to create EntityManager instances.

Parameters

mixed $conn An array with the connection parameters or an existing Connection instance.
Configuration $config The Configuration instance to use.
EventManager $eventManager The EventManager instance to use.

Return Value

EntityManager The created EntityManager.

Exceptions

InvalidArgumentException
ORMException

FilterCollection getFilters()

Gets the enabled filters.

Return Value

FilterCollection The active filter collection.

boolean isFiltersStateClean()

Checks whether the state of the filter collection is clean.

Return Value

boolean True, if the filter collection is clean.

boolean hasFilters()

Checks whether the Entity Manager has filters.

Return Value

boolean True, if the EM has a filter collection.