Documentation

EntityManager
in package
implements EntityManagerInterface

The EntityManager is the central access point to ORM functionality.

It is a facade to all different ORM subsystems such as UnitOfWork, Query Language and Repository API. Instantiation is done through the static create() method. The quickest way to obtain a fully configured EntityManager is:

use Doctrine\ORM\Tools\ORMSetup;
use Doctrine\ORM\EntityManager;

$paths = ['/path/to/entity/mapping/files'];

$config = ORMSetup::createAttributeMetadataConfiguration($paths);
$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true], $config);
$entityManager = new EntityManager($connection, $config);

For more information see

You should never attempt to inherit from the EntityManager: Inheritance is not a valid extension point for the EntityManager. Instead you should take a look at the EntityManagerDecorator and wrap your entity manager in a decorator.

Tags
final

Table of Contents

Interfaces

EntityManagerInterface
EntityManager interface

Properties

$cache  : Cache|null
The second level cache regions API.
$closed  : bool
Whether the EntityManager is closed or not.
$config  : Configuration
The used Configuration.
$conn  : Connection
The database connection used by the EntityManager.
$eventManager  : EventManager
The event manager that is the central point of the event system.
$expressionBuilder  : Expr|null
The expression builder instance used to generate query expressions.
$filterCollection  : FilterCollection|null
Collection of query filters.
$metadataFactory  : ClassMetadataFactory
The metadata factory, used to retrieve the ORM metadata of entity classes.
$proxyFactory  : ProxyFactory
The proxy factory used to create dynamic proxies.
$repositoryFactory  : RepositoryFactory
The repository factory used to create dynamic repositories.
$unitOfWork  : UnitOfWork
The UnitOfWork used to coordinate object-level transactions.

Methods

__construct()  : mixed
Creates a new EntityManager that operates on the given database connection and uses the given Configuration and EventManager implementations.
beginTransaction()  : void
Starts a transaction on the underlying database connection.
clear()  : void
Clears the EntityManager. All entities that are currently managed by this EntityManager become detached.
close()  : void
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.
commit()  : void
Commits a transaction on the underlying database connection.
contains()  : bool
Determines whether an entity instance is managed in this EntityManager.
copy()  : object
Creates a copy of the given entity. Can create a shallow or a deep copy.
create()  : EntityManager
Factory method to create EntityManager instances.
createNamedNativeQuery()  : NativeQuery
Creates a NativeQuery from a named native query.
createNamedQuery()  : Query
Creates a Query from a named query.
createNativeQuery()  : NativeQuery
Creates a native SQL query.
createQuery()  : Query
Creates a new Query object.
createQueryBuilder()  : QueryBuilder
Create a QueryBuilder instance
detach()  : void
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.
find()  : object|null
Finds an Entity by its identifier.
flush()  : void
Flushes all changes to objects that have been queued up to now to the database.
getCache()  : Cache|null
Returns the cache API for managing the second level cache regions or NULL if the cache is not enabled.
getClassMetadata()  : ClassMetadata
Returns the ORM metadata descriptor for a class.
getConfiguration()  : Configuration
Gets the Configuration used by the EntityManager.
getConnection()  : Connection
Gets the database connection object used by the EntityManager.
getEventManager()  : EventManager
Gets the EventManager used by the EntityManager.
getExpressionBuilder()  : Expr
Gets an ExpressionBuilder used for object-oriented construction of query expressions.
getFilters()  : FilterCollection
Gets the enabled filters.
getHydrator()  : AbstractHydrator
Gets a hydrator for the given hydration mode.
getMetadataFactory()  : ClassMetadataFactory
Gets the metadata factory used to gather the metadata of classes.
getPartialReference()  : object|null
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.
getProxyFactory()  : ProxyFactory
Gets the proxy factory used by the EntityManager to create entity proxies.
getReference()  : object|null
Gets a reference to the entity identified by the given type and identifier without actually loading it, if the entity is not yet loaded.
getRepository()  : ObjectRepository|EntityRepository
Gets the repository for an entity class.
getUnitOfWork()  : UnitOfWork
Gets the UnitOfWork used by the EntityManager to coordinate operations.
hasFilters()  : bool
Checks whether the Entity Manager has filters.
initializeObject()  : mixed
{@inheritDoc}
isFiltersStateClean()  : bool
Checks whether the state of the filter collection is clean.
isOpen()  : bool
Check if the Entity manager is open or closed.
lock()  : void
Acquire a lock on the given entity.
merge()  : object
Merges the state of a detached entity into the persistence context of this EntityManager and returns the managed copy of the entity.
newHydrator()  : AbstractHydrator
Create a new instance for the given hydration mode.
persist()  : void
Tells the EntityManager to make an instance managed and persistent.
refresh()  : void
Refreshes the persistent state of an entity from the database, overriding any local changes that have not yet been persisted.
remove()  : void
Removes an entity instance.
rollback()  : void
Performs a rollback on the underlying database connection.
transactional()  : mixed
Executes a function in a transaction.
wrapInTransaction()  : mixed
{@inheritDoc}
createConnection()  : Connection
Factory method to create Connection instances.
checkLockRequirements()  : void
configureLegacyMetadataCache()  : void
configureMetadataCache()  : void
errorIfClosed()  : void
Throws an exception if the EntityManager is closed or currently not active.

Properties

$closed

Whether the EntityManager is closed or not.

private bool $closed = false

$conn

The database connection used by the EntityManager.

private Connection $conn

$expressionBuilder

The expression builder instance used to generate query expressions.

private Expr|null $expressionBuilder

Methods

__construct()

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

public __construct(Connection $conn, Configuration $config[, EventManager|null $eventManager = null ]) : mixed
Parameters
$conn : Connection
$config : Configuration
$eventManager : EventManager|null = null

beginTransaction()

Starts a transaction on the underlying database connection.

public beginTransaction() : void

clear()

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

public clear([string|null $entityName = null ]) : void
Parameters
$entityName : string|null = null

if given, only entities of this type will get detached

Tags
throws
ORMInvalidArgumentException

If a non-null non-string value is given.

throws
MappingException

If a $entityName is given, but that entity is not found in the mappings.

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.

public close() : void

commit()

Commits a transaction on the underlying database connection.

public commit() : void

contains()

Determines whether an entity instance is managed in this EntityManager.

public contains(object $entity) : bool
Parameters
$entity : object
Return values
bool

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

copy()

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

public copy(mixed $entity[, mixed $deep = false ]) : object
Parameters
$entity : mixed

The entity to copy.

$deep : mixed = false

FALSE for a shallow copy, TRUE for a deep copy.

Tags
psalm-return

never

Return values
object

The new entity.

create()

Factory method to create EntityManager instances.

public static create(array<string|int, mixed>|Connection $connection, Configuration $config[, EventManager|null $eventManager = null ]) : EntityManager
Parameters
$connection : array<string|int, mixed>|Connection

An array with the connection parameters or an existing Connection instance.

$config : Configuration

The Configuration instance to use.

$eventManager : EventManager|null = null

The EventManager instance to use.

Tags
deprecated

Use DriverManager::getConnection() to bootstrap the connection and call the constructor.

psalm-param

array<string, mixed>|Connection $connection

throws
InvalidArgumentException
throws
ORMException
Return values
EntityManager

The created EntityManager.

createNamedNativeQuery()

Creates a NativeQuery from a named native query.

public createNamedNativeQuery(mixed $name) : NativeQuery
Parameters
$name : mixed
Return values
NativeQuery

createNamedQuery()

Creates a Query from a named query.

public createNamedQuery(mixed $name) : Query
Parameters
$name : mixed
Return values
Query

createQuery()

Creates a new Query object.

public createQuery([mixed $dql = '' ]) : Query
Parameters
$dql : mixed = ''

The DQL string.

Return values
Query

detach()

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.

public detach(object $entity) : void

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

Parameters
$entity : object

The entity to detach.

Tags
throws
ORMInvalidArgumentException

find()

Finds an Entity by its identifier.

public find(string $className, mixed $id[, int|null $lockMode = null ][, int|null $lockVersion = null ]) : object|null
Parameters
$className : string

The class name of the entity to find.

$id : mixed

The identity of the entity to find.

$lockMode : int|null = null

One of the \Doctrine\DBAL\LockMode::* constants or NULL if no specific lock mode should be used during the search.

$lockVersion : int|null = null

The version of the entity to find when using optimistic locking.

Tags
psalm-param

class-string<T> $className

psalm-param

LockMode::*|null $lockMode

psalm-return

?T

throws
OptimisticLockException
throws
ORMInvalidArgumentException
throws
TransactionRequiredException
throws
ORMException
template

T

Return values
object|null

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

flush()

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

public flush([object|array<string|int, mixed>|null $entity = null ]) : void

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
$entity : object|array<string|int, mixed>|null = null
Tags
throws
OptimisticLockException

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

throws
ORMException

getCache()

Returns the cache API for managing the second level cache regions or NULL if the cache is not enabled.

public getCache() : Cache|null
Return values
Cache|null

getClassMetadata()

Returns the ORM metadata descriptor for a class.

public getClassMetadata(mixed $className) : ClassMetadata

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

Internal note: Performance-sensitive method.

Parameters
$className : mixed
Return values
ClassMetadata

getConnection()

Gets the database connection object used by the EntityManager.

public getConnection() : Connection
Return values
Connection

getExpressionBuilder()

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

public getExpressionBuilder() : Expr
Return values
Expr

getPartialReference()

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.

public getPartialReference(mixed $entityName, mixed $identifier) : object|null
Parameters
$entityName : mixed

The name of the entity type.

$identifier : mixed

The entity identifier.

Return values
object|null

The (partial) entity reference

getReference()

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

public getReference(mixed $entityName, mixed $id) : object|null
Parameters
$entityName : mixed

The name of the entity type.

$id : mixed

The entity identifier.

Return values
object|null

The entity reference.

hasFilters()

Checks whether the Entity Manager has filters.

public hasFilters() : bool
Return values
bool

True, if the EM has a filter collection.

initializeObject()

{@inheritDoc}

public initializeObject(mixed $obj) : mixed
Parameters
$obj : mixed

isFiltersStateClean()

Checks whether the state of the filter collection is clean.

public isFiltersStateClean() : bool
Return values
bool

True, if the filter collection is clean.

isOpen()

Check if the Entity manager is open or closed.

public isOpen() : bool
Return values
bool

lock()

Acquire a lock on the given entity.

public lock(mixed $entity, mixed $lockMode[, mixed $lockVersion = null ]) : void
Parameters
$entity : mixed
$lockMode : mixed
$lockVersion : mixed = null

merge()

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

public merge(object $entity) : object

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

Parameters
$entity : object

The detached entity to merge into the persistence context.

Tags
deprecated
2.7

This method is being removed from the ORM and won't have any replacement

throws
ORMInvalidArgumentException
throws
ORMException
Return values
object

The managed copy of the entity.

persist()

Tells the EntityManager to make an instance managed and persistent.

public persist(object $entity) : void

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
$entity : object

The instance to make managed and persistent.

Tags
throws
ORMInvalidArgumentException
throws
ORMException

refresh()

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

public refresh(object $entity[, int|null $lockMode = null ]) : void
Parameters
$entity : object

The entity to refresh

$lockMode : int|null = null
Tags
psalm-param

LockMode::*|null $lockMode

throws
ORMInvalidArgumentException
throws
ORMException
throws
TransactionRequiredException

remove()

Removes an entity instance.

public remove(object $entity) : void

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

Parameters
$entity : object

The entity instance to remove.

Tags
throws
ORMInvalidArgumentException
throws
ORMException

rollback()

Performs a rollback on the underlying database connection.

public rollback() : void

transactional()

Executes a function in a transaction.

public transactional(mixed $func) : mixed
Parameters
$func : mixed

The function to execute transactionally.

Return values
mixed

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

wrapInTransaction()

{@inheritDoc}

public wrapInTransaction(callable $func) : mixed
Parameters
$func : callable

createConnection()

Factory method to create Connection instances.

protected static createConnection(array<string|int, mixed>|Connection $connection, Configuration $config[, EventManager|null $eventManager = null ]) : Connection
Parameters
$connection : array<string|int, mixed>|Connection

An array with the connection parameters or an existing Connection instance.

$config : Configuration

The Configuration instance to use.

$eventManager : EventManager|null = null

The EventManager instance to use.

Tags
deprecated

Use DriverManager::getConnection() to bootstrap the connection.

psalm-param

array<string, mixed>|Connection $connection

throws
InvalidArgumentException
throws
ORMException
Return values
Connection

configureLegacyMetadataCache()

private configureLegacyMetadataCache() : void

configureMetadataCache()

private configureMetadataCache() : void

errorIfClosed()

Throws an exception if the EntityManager is closed or currently not active.

private errorIfClosed() : void
Tags
throws
EntityManagerClosed

If the EntityManager is closed.


        
On this page

Search results