» API Reference » ORM\EntityManager
ORM\EntityManager
Table of Contents
Abstract
The EntityManager that manages the instances of Entities.
Constants
Name |
Value |
OPT_CONNECTION |
'connection' |
OPT_TABLE_NAME_TEMPLATE |
'tableNameTemplate' |
OPT_NAMING_SCHEME_TABLE |
'namingSchemeTable' |
OPT_NAMING_SCHEME_COLUMN |
'namingSchemeColumn' |
OPT_NAMING_SCHEME_METHODS |
'namingSchemeMethods' |
OPT_NAMING_SCHEME_ATTRIBUTE |
'namingSchemeAttribute' |
OPT_QUOTING_CHARACTER |
'quotingChar' |
OPT_IDENTIFIER_DIVIDER |
'identifierDivider' |
OPT_BOOLEAN_TRUE |
'true' |
OPT_BOOLEAN_FALSE |
'false' |
OPT_DBAL_CLASS |
'dbalClass' |
OPT_MYSQL_BOOLEAN_TRUE |
'mysqlTrue' |
OPT_MYSQL_BOOLEAN_FALSE |
'mysqlFalse' |
OPT_SQLITE_BOOLEAN_TRUE |
'sqliteTrue' |
OPT_SQLITE_BOOLEAN_FALSE |
'sqliteFalse' |
OPT_PGSQL_BOOLEAN_TRUE |
'pgsqlTrue' |
OPT_PGSQL_BOOLEAN_FALSE |
'pgsqlFalse' |
Methods
ORM\EntityManager::__construct
public function __construct(array $options = [])
Constructor
Parameters
Parameter |
Type |
Description |
$options |
array |
Options for the new EntityManager |
ORM\EntityManager::__construct
↑ top
ORM\EntityManager::beginTransaction
public function beginTransaction(): bool
Begin a transaction or create a savepoint
ORM\EntityManager::beginTransaction
↑ top
ORM\EntityManager::commit
public function commit(bool $all = false): bool
Commit the current transaction or decrease the savepoint counter
Actually nothing will be committed if there are savepoints. Instead the counter will be decreased and
the commited savepoint will still be rolled back when you call rollback afterwards.
Hopefully that gives a hint why save points are no transactions and what the limitations are.
Begin transaction
updates / inserts for transaction1
Create savepoint transaction1
updates / inserts for transaction2
Create savepoint transaction2
updates / inserts for transaction3
<no commit here but you called commit for transaction3>
updates / inserts for transaction2
rollback of transaction2 to savepoint of transaction1
update / inserts for transaction1
commit of transaction1
Parameters
Parameter |
Type |
Description |
$all |
bool |
Commit all opened transactions and savepoints |
ORM\EntityManager::commit
↑ top
ORM\EntityManager::defineForNamespace
public function defineForNamespace(mixed $nameSpace): $this
Define $this EntityManager as the default EntityManager for $nameSpace
Parameters
Parameter |
Type |
Description |
$nameSpace |
mixed |
|
ORM\EntityManager::defineForNamespace
↑ top
ORM\EntityManager::defineForParent
public function defineForParent(mixed $class): $this
Define $this EntityManager as the default EntityManager for subClasses of $class
Parameters
Parameter |
Type |
Description |
$class |
mixed |
|
ORM\EntityManager::defineForParent
↑ top
ORM\EntityManager::delete
public function delete(\ORM\Entity $entity): bool
Delete $entity from database
This method does not delete from the map - you can still receive the entity via fetch.
Parameters
ORM\EntityManager::delete
↑ top
ORM\EntityManager::describe
public function describe(string $table): \ORM\Dbal\Column[]|\ORM\Dbal\Table
Returns an array of columns from $table.
Parameters
Parameter |
Type |
Description |
$table |
string |
|
ORM\EntityManager::describe
↑ top
ORM\EntityManager::detach
public function detach(
\ORM\ObserverInterface $observer,
string|null $from = null,
): bool
Detach $observer from all classes
If the observer is attached to multiple classes all are removed except the optional parameter
$from defines from which class to remove the $observer.
Returns whether or not an observer got detached.
Parameters
ORM\EntityManager::detach
↑ top
ORM\EntityManager::eagerLoad
public function eagerLoad(string $relation, \ORM\Entity $entities): mixed
Load the $relation on all $entities with the least amount of queries
$relation can be nested by dividing them with a dot.
Parameters
Parameter |
Type |
Description |
$relation |
string |
|
$entities |
ORM\Entity |
|
ORM\EntityManager::eagerLoad
↑ top
ORM\EntityManager::escapeIdentifier
public function escapeIdentifier(string $identifier): string
Returns $identifier quoted for use in a sql statement
Parameters
Parameter |
Type |
Description |
$identifier |
string |
Identifier to quote |
ORM\EntityManager::escapeIdentifier
↑ top
ORM\EntityManager::escapeValue
public function escapeValue(mixed $value): string
Parameters
Parameter |
Type |
Description |
$value |
mixed |
The variable that should be returned in SQL syntax |
ORM\EntityManager::escapeValue
↑ top
ORM\EntityManager::fetch
public function fetch(
string $class,
mixed $primaryKey = null,
): \ORM\Entity|\ORM\EntityFetcher
Fetch one or more entities
With $primaryKey it tries to find this primary key in the entity map (carefully: mostly the database returns a
string and we do not convert them). If there is no entity in the entity map it tries to fetch the entity from
the database. The return value is then null (not found) or the entity.
Without $primaryKey it creates an entityFetcher and returns this.
Parameters
Parameter |
Type |
Description |
$class |
string |
The entity class you want to fetch |
$primaryKey |
mixed |
The primary key of the entity you want to fetch |
ORM\EntityManager::fetch
↑ top
ORM\EntityManager::finishBulkInserts
public function finishBulkInserts(string $class): \ORM\Entity[]
Finish the bulk insert for $class.
Returns an array of entities added.
Parameters
Parameter |
Type |
Description |
$class |
string |
|
ORM\EntityManager::finishBulkInserts
↑ top
ORM\EntityManager::fire
public function fire(\ORM\Event $event): bool
Fire $event on $entity
Parameters
ORM\EntityManager::fire
↑ top
ORM\EntityManager::getConnection
public function getConnection(): \PDO
Get the pdo connection.
ORM\EntityManager::getConnection
↑ top
ORM\EntityManager::getDbal
public function getDbal(): \ORM\Dbal\Dbal
Get the Database Abstraction Layer
ORM\EntityManager::getDbal
↑ top
ORM\EntityManager::getInstance
public static function getInstance(string $class = null): \ORM\EntityManager
Get an instance of the EntityManager.
If no class is given it gets $class from backtrace.
It first tries to get the EntityManager for the Namespace of $class, then for the parents of $class. If no
EntityManager is found it returns the last created EntityManager (null if no EntityManager got created).
Static |
|
This method is static. |
Returns |
ORM\EntityManager |
The entity manager instance to use for $class |
Throws |
ORM\Exception |
When no entity manager got initialized |
Parameters
Parameter |
Type |
Description |
$class |
string |
|
ORM\EntityManager::getInstance
↑ top
ORM\EntityManager::getNamer
public function getNamer(): \ORM\Namer
Get the Namer instance
ORM\EntityManager::getNamer
↑ top
ORM\EntityManager::getOption
public function getOption(mixed $option): mixed
Get $option
Parameters
Parameter |
Type |
Description |
$option |
mixed |
|
ORM\EntityManager::getOption
↑ top
ORM\EntityManager::has
public function has(\ORM\Entity|string $entity, mixed $primaryKey = null): bool
Check if the entity map has $entity
If you want to know if the entity already exists in the map use this method.
Parameters
Parameter |
Type |
Description |
|
$entity |
**ORM\Entity |
string** |
|
$primaryKey |
mixed |
|
|
ORM\EntityManager::has
↑ top
ORM\EntityManager::map
public function map(
\ORM\Entity $entity,
bool $update = false,
string $class = null,
): \ORM\Entity
Map $entity in the entity map
Returns the given entity or an entity that previously got mapped. This is useful to work in every function with
the same object.
$user = $enitityManager->map(new User(['id' => 42]));
Parameters
Parameter |
Type |
Description |
$entity |
ORM\Entity |
|
$update |
bool |
Update the entity map |
$class |
string |
Overwrite the class |
ORM\EntityManager::map
↑ top
ORM\EntityManager::observe
public function observe(
string $class,
\ORM\ObserverInterface|null $observer = null,
): \ORM\Observer\CallbackObserver|null
Observe $class using $observer
If AbstractObserver is omitted it returns a new CallbackObserver. Usage example:
$em->observe(User::class)
->on('inserted', function (User $user) { ... })
->on('deleted', function (User $user) { ... });
For more information about model events please consult the [documentation](https://tflori.github.io/
Parameters
ORM\EntityManager::observe
↑ top
ORM\EntityManager::query
public function query(
string $table,
string $alias = '',
): \ORM\QueryBuilder\QueryBuilder
Get a query builder for $table
Parameters
Parameter |
Type |
Description |
$table |
string |
|
$alias |
string |
|
ORM\EntityManager::query
↑ top
ORM\EntityManager::raw
public static function raw(string $expression): \ORM\Dbal\Expression
Create a raw expression from $expression to disable escaping
Parameters
Parameter |
Type |
Description |
$expression |
string |
|
ORM\EntityManager::raw
↑ top
ORM\EntityManager::rollback
public function rollback(): bool
Rollback the current transaction or save point
ORM\EntityManager::rollback
↑ top
ORM\EntityManager::setConnection
public function setConnection(mixed $connection): mixed
Add connection after instantiation
The connection can be an array of parameters for DbConfig::__construct(), a callable function that returns a PDO
instance, an instance of DbConfig or a PDO instance itself.
When it is not a PDO instance the connection get established on first use.
Parameters
Parameter |
Type |
Description |
$connection |
mixed |
A configuration for (or a) PDO instance |
ORM\EntityManager::setConnection
↑ top
ORM\EntityManager::setOption
public function setOption(string $option, mixed $value): $this
Set $option to $value
Parameters
Parameter |
Type |
Description |
$option |
string |
One of OPT_* constants |
$value |
mixed |
|
ORM\EntityManager::setOption
↑ top
ORM\EntityManager::setResolver
public static function setResolver(callable $resolver): mixed
Overwrite the functionality of ::getInstance($class) by $resolver($class)
Static |
|
This method is static. |
Returns |
mixed |
|
Parameters
Parameter |
Type |
Description |
$resolver |
callable |
|
ORM\EntityManager::setResolver
↑ top
ORM\EntityManager::sync
public function sync(\ORM\Entity $entity, bool $reset = false): bool
Synchronizing $entity with database
If $reset is true it also calls reset() on $entity.
Parameters
Parameter |
Type |
Description |
$entity |
ORM\Entity |
|
$reset |
bool |
Reset entities current data |
ORM\EntityManager::sync
↑ top
ORM\EntityManager::useBulkInserts
public function useBulkInserts(string $class, int $limit = 20): \ORM\BulkInsert
Force $class to use bulk insert.
At the end you should call finish bulk insert otherwise you may loose data.
Parameters
Parameter |
Type |
Description |
$class |
string |
|
$limit |
int |
Maximum number of rows per insert |
ORM\EntityManager::useBulkInserts
↑ top