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);
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
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
$cache
The second level cache regions API.
private
Cache|null
$cache
$closed
Whether the EntityManager is closed or not.
private
bool
$closed
= false
$config
The used Configuration.
private
Configuration
$config
$conn
The database connection used by the EntityManager.
private
Connection
$conn
$eventManager
The event manager that is the central point of the event system.
private
EventManager
$eventManager
$expressionBuilder
The expression builder instance used to generate query expressions.
private
Expr|null
$expressionBuilder
$filterCollection
Collection of query filters.
private
FilterCollection|null
$filterCollection
$metadataFactory
The metadata factory, used to retrieve the ORM metadata of entity classes.
private
ClassMetadataFactory
$metadataFactory
$proxyFactory
The proxy factory used to create dynamic proxies.
private
ProxyFactory
$proxyFactory
$repositoryFactory
The repository factory used to create dynamic repositories.
private
RepositoryFactory
$repositoryFactory
$unitOfWork
The UnitOfWork used to coordinate object-level transactions.
private
UnitOfWork
$unitOfWork
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
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
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
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
NativeQuerycreateNamedQuery()
Creates a Query from a named query.
public
createNamedQuery(mixed $name) : Query
Parameters
- $name : mixed
Return values
QuerycreateNativeQuery()
Creates a native SQL query.
public
createNativeQuery(mixed $sql, ResultSetMapping $rsm) : NativeQuery
Parameters
- $sql : mixed
- $rsm : ResultSetMapping
-
The ResultSetMapping to use.
Return values
NativeQuerycreateQuery()
Creates a new Query object.
public
createQuery([mixed $dql = '' ]) : Query
Parameters
- $dql : mixed = ''
-
The DQL string.
Return values
QuerycreateQueryBuilder()
Create a QueryBuilder instance
public
createQueryBuilder() : QueryBuilder
Return values
QueryBuilderdetach()
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
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
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
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|nullgetClassMetadata()
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
ClassMetadatagetConfiguration()
Gets the Configuration used by the EntityManager.
public
getConfiguration() : Configuration
Return values
ConfigurationgetConnection()
Gets the database connection object used by the EntityManager.
public
getConnection() : Connection
Return values
ConnectiongetEventManager()
Gets the EventManager used by the EntityManager.
public
getEventManager() : EventManager
Return values
EventManagergetExpressionBuilder()
Gets an ExpressionBuilder used for object-oriented construction of query expressions.
public
getExpressionBuilder() : Expr
Return values
ExprgetFilters()
Gets the enabled filters.
public
getFilters() : FilterCollection
Return values
FilterCollection —The active filter collection.
getHydrator()
Gets a hydrator for the given hydration mode.
public
getHydrator(mixed $hydrationMode) : AbstractHydrator
Parameters
- $hydrationMode : mixed
Return values
AbstractHydratorgetMetadataFactory()
Gets the metadata factory used to gather the metadata of classes.
public
getMetadataFactory() : ClassMetadataFactory
Return values
ClassMetadataFactorygetPartialReference()
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
getProxyFactory()
Gets the proxy factory used by the EntityManager to create entity proxies.
public
getProxyFactory() : ProxyFactory
Return values
ProxyFactorygetReference()
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.
getRepository()
Gets the repository for an entity class.
public
getRepository(string $entityName) : ObjectRepository|EntityRepository
Parameters
- $entityName : string
-
The name of the entity.
Tags
Return values
ObjectRepository|EntityRepository —The repository class.
getUnitOfWork()
Gets the UnitOfWork used by the EntityManager to coordinate operations.
public
getUnitOfWork() : UnitOfWork
Return values
UnitOfWorkhasFilters()
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
boollock()
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
Return values
object —The managed copy of the entity.
newHydrator()
Create a new instance for the given hydration mode.
public
newHydrator(mixed $hydrationMode) : AbstractHydrator
Parameters
- $hydrationMode : mixed
Return values
AbstractHydratorpersist()
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
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
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
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
Return values
ConnectioncheckLockRequirements()
private
checkLockRequirements(int $lockMode, ClassMetadata $class) : void
Parameters
- $lockMode : int
- $class : ClassMetadata
Tags
configureLegacyMetadataCache()
private
configureLegacyMetadataCache() : void
configureMetadataCache()
private
configureMetadataCache() : void
errorIfClosed()
Throws an exception if the EntityManager is closed or currently not active.
private
errorIfClosed() : void