Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions
EntityManager Class Reference

Public Member Functions

 __construct (OperationPool $operationPool, MetadataPool $metadataPool, TypeResolver $typeResolver, CallbackHandler $callbackHandler)
 
 load ($entity, $identifier, $arguments=[])
 
 save ($entity, $arguments=[])
 
 has ($entity)
 
 delete ($entity, $arguments=[])
 

Detailed Description

It's not recommended to use EntityManager and its infrastructure for your entities persistence. In the nearest future new Persistence Entity Manager would be released which will cover all the requirements for persistence layer along with Query API as performance efficient APIs for Read scenarios.

Currently, it's recommended to use Resource Model infrastructure and make a successor of Magento\Framework\Model\ResourceModel\Db\AbstractDb class or successor of Magento\Eav\Model\Entity\AbstractEntity if EAV attributes support needed.

For filtering operations, it's recommended to use successor of Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection class.

Definition at line 26 of file EntityManager.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( OperationPool  $operationPool,
MetadataPool  $metadataPool,
TypeResolver  $typeResolver,
CallbackHandler  $callbackHandler 
)
Parameters
OperationPool$operationPool
MetadataPool$metadataPool
TypeResolver$typeResolver
CallbackHandler$callbackHandler

Definition at line 44 of file EntityManager.php.

49  {
50  $this->operationPool = $operationPool;
51  $this->metadataPool = $metadataPool;
52  $this->typeResolver = $typeResolver;
53  $this->callbackHandler = $callbackHandler;
54  }

Member Function Documentation

◆ delete()

delete (   $entity,
  $arguments = [] 
)
Parameters
object$entity
array$arguments
Returns
bool
Exceptions

Definition at line 127 of file EntityManager.php.

128  {
129  $entityType = $this->typeResolver->resolve($entity);
130  $operation = $this->operationPool->getOperation($entityType, 'delete');
131  if (!($operation instanceof DeleteInterface)) {
132  throw new \LogicException(get_class($operation) . ' must implement ' . DeleteInterface::class);
133  }
134  try {
135  $operation->execute($entity, $arguments);
136  $this->callbackHandler->process($entityType);
137  } catch (\Exception $e) {
138  $this->callbackHandler->clear($entityType);
139  throw $e;
140  }
141  return true;
142  }
$entity
Definition: element.phtml:22
$arguments

◆ has()

has (   $entity)
Parameters
object$entity
Returns
bool
Exceptions

Definition at line 110 of file EntityManager.php.

111  {
112  $entityType = $this->typeResolver->resolve($entity);
113  $operation = $this->operationPool->getOperation($entityType, 'checkIfExists');
114  if (!($operation instanceof CheckIfExistsInterface)) {
115  throw new \LogicException(get_class($operation) . ' must implement ' . CheckIfExistsInterface::class);
116  }
117  return $operation->execute($entity);
118  }
$entity
Definition: element.phtml:22

◆ load()

load (   $entity,
  $identifier,
  $arguments = [] 
)
Parameters
object$entity
string$identifier
array$arguments
Returns
mixed
Exceptions

Definition at line 63 of file EntityManager.php.

64  {
65  $entityType = $this->typeResolver->resolve($entity);
66  $operation = $this->operationPool->getOperation($entityType, 'read');
67  if (!($operation instanceof ReadInterface)) {
68  throw new \LogicException(get_class($operation) . ' must implement ' . ReadInterface::class);
69  }
70  $entity = $operation->execute($entity, $identifier, $arguments);
71  return $entity;
72  }
$entity
Definition: element.phtml:22
$arguments

◆ save()

save (   $entity,
  $arguments = [] 
)
Parameters
object$entity
array$arguments
Returns
object
Exceptions

Definition at line 81 of file EntityManager.php.

82  {
83  $entityType = $this->typeResolver->resolve($entity);
84  if ($this->has($entity)) {
85  $operation = $this->operationPool->getOperation($entityType, 'update');
86  if (!($operation instanceof UpdateInterface)) {
87  throw new \LogicException(get_class($operation) . ' must implement ' . UpdateInterface::class);
88  }
89  } else {
90  $operation = $this->operationPool->getOperation($entityType, 'create');
91  if (!($operation instanceof CreateInterface)) {
92  throw new \LogicException(get_class($operation) . ' must implement ' . CreateInterface::class);
93  }
94  }
95  try {
96  $entity = $operation->execute($entity, $arguments);
97  $this->callbackHandler->process($entityType);
98  } catch (\Exception $e) {
99  $this->callbackHandler->clear($entityType);
100  throw $e;
101  }
102  return $entity;
103  }
$entity
Definition: element.phtml:22
$arguments

The documentation for this class was generated from the following file: