Moved implementation from EntityRepository to EntityManager. This decouples ER implementation from EM, as it should be.
This commit is contained in:
parent
93cef61270
commit
e5979b5ef2
@ -354,21 +354,82 @@ class EntityManager implements ObjectManager
|
|||||||
|
|
||||||
$this->unitOfWork->commit($entity);
|
$this->unitOfWork->commit($entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds an Entity by its identifier.
|
* Finds an Entity by its identifier.
|
||||||
*
|
*
|
||||||
* This is just a convenient shortcut for getRepository($entityName)->find($id).
|
|
||||||
*
|
|
||||||
* @param string $entityName
|
* @param string $entityName
|
||||||
* @param mixed $identifier
|
* @param mixed $id
|
||||||
* @param int $lockMode
|
* @param integer $lockMode
|
||||||
* @param int $lockVersion
|
* @param integer $lockVersion
|
||||||
|
*
|
||||||
* @return object
|
* @return object
|
||||||
*/
|
*/
|
||||||
public function find($entityName, $identifier, $lockMode = LockMode::NONE, $lockVersion = null)
|
public function find($entityName, $id, $lockMode = LockMode::NONE, $lockVersion = null)
|
||||||
{
|
{
|
||||||
return $this->getRepository($entityName)->find($identifier, $lockMode, $lockVersion);
|
$class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\'));
|
||||||
|
|
||||||
|
if ( ! is_array($id)) {
|
||||||
|
$id = array($class->identifier[0] => $id);
|
||||||
|
}
|
||||||
|
|
||||||
|
$sortedId = array();
|
||||||
|
|
||||||
|
foreach ($class->identifier as $identifier) {
|
||||||
|
if ( ! isset($id[$identifier])) {
|
||||||
|
throw ORMException::missingIdentifierField($class->name, $identifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
$sortedId[$identifier] = $id[$identifier];
|
||||||
|
}
|
||||||
|
|
||||||
|
$unitOfWork = $this->getUnitOfWork();
|
||||||
|
|
||||||
|
// Check identity map first
|
||||||
|
if (($entity = $unitOfWork->tryGetById($sortedId, $class->rootEntityName)) !== false) {
|
||||||
|
if ( ! ($entity instanceof $class->name)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch ($lockMode) {
|
||||||
|
case LockMode::OPTIMISTIC:
|
||||||
|
$this->lock($entity, $lockMode, $lockVersion);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case LockMode::PESSIMISTIC_READ:
|
||||||
|
case LockMode::PESSIMISTIC_WRITE:
|
||||||
|
$persister = $unitOfWork->getEntityPersister($class->name);
|
||||||
|
$persister->refresh($sortedId, $entity, $lockMode);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $entity; // Hit!
|
||||||
|
}
|
||||||
|
|
||||||
|
$persister = $unitOfWork->getEntityPersister($class->name);
|
||||||
|
|
||||||
|
switch ($lockMode) {
|
||||||
|
case LockMode::NONE:
|
||||||
|
return $persister->load($sortedId);
|
||||||
|
|
||||||
|
case LockMode::OPTIMISTIC:
|
||||||
|
if ( ! $class->isVersioned) {
|
||||||
|
throw OptimisticLockException::notVersioned($class->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
$entity = $persister->load($sortedId);
|
||||||
|
|
||||||
|
$unitOfWork->lock($entity, $lockMode, $lockVersion);
|
||||||
|
|
||||||
|
return $entity;
|
||||||
|
|
||||||
|
default:
|
||||||
|
if ( ! $this->getConnection()->isTransactionActive()) {
|
||||||
|
throw TransactionRequiredException::transactionRequired();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $persister->load($sortedId, null, null, array(), $lockMode);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -125,71 +125,15 @@ class EntityRepository implements ObjectRepository, Selectable
|
|||||||
/**
|
/**
|
||||||
* Finds an entity by its primary key / identifier.
|
* Finds an entity by its primary key / identifier.
|
||||||
*
|
*
|
||||||
* @param $id The identifier.
|
* @param mixed $id The identifier.
|
||||||
* @param int $lockMode
|
* @param integer $lockMode
|
||||||
* @param int $lockVersion
|
* @param integer $lockVersion
|
||||||
|
*
|
||||||
* @return object The entity.
|
* @return object The entity.
|
||||||
*/
|
*/
|
||||||
public function find($id, $lockMode = LockMode::NONE, $lockVersion = null)
|
public function find($id, $lockMode = LockMode::NONE, $lockVersion = null)
|
||||||
{
|
{
|
||||||
if ( ! is_array($id)) {
|
return $this->_em->find($this->_entityName, $id, $lockMode, $lockVersion);
|
||||||
$id = array($this->_class->identifier[0] => $id);
|
|
||||||
}
|
|
||||||
|
|
||||||
$sortedId = array();
|
|
||||||
|
|
||||||
foreach ($this->_class->identifier as $identifier) {
|
|
||||||
if ( ! isset($id[$identifier])) {
|
|
||||||
throw ORMException::missingIdentifierField($this->_class->name, $identifier);
|
|
||||||
}
|
|
||||||
|
|
||||||
$sortedId[$identifier] = $id[$identifier];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check identity map first
|
|
||||||
if (($entity = $this->_em->getUnitOfWork()->tryGetById($sortedId, $this->_class->rootEntityName)) !== false) {
|
|
||||||
if ( ! ($entity instanceof $this->_class->name)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch ($lockMode) {
|
|
||||||
case LockMode::OPTIMISTIC:
|
|
||||||
$this->_em->lock($entity, $lockMode, $lockVersion);
|
|
||||||
break;
|
|
||||||
case LockMode::PESSIMISTIC_READ:
|
|
||||||
case LockMode::PESSIMISTIC_WRITE:
|
|
||||||
$persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName);
|
|
||||||
$persister->refresh($sortedId, $entity, $lockMode);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $entity; // Hit!
|
|
||||||
}
|
|
||||||
|
|
||||||
$persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName);
|
|
||||||
|
|
||||||
switch ($lockMode) {
|
|
||||||
case LockMode::NONE:
|
|
||||||
return $persister->load($sortedId);
|
|
||||||
|
|
||||||
case LockMode::OPTIMISTIC:
|
|
||||||
if ( ! $this->_class->isVersioned) {
|
|
||||||
throw OptimisticLockException::notVersioned($this->_entityName);
|
|
||||||
}
|
|
||||||
|
|
||||||
$entity = $persister->load($sortedId);
|
|
||||||
|
|
||||||
$this->_em->getUnitOfWork()->lock($entity, $lockMode, $lockVersion);
|
|
||||||
|
|
||||||
return $entity;
|
|
||||||
|
|
||||||
default:
|
|
||||||
if ( ! $this->_em->getConnection()->isTransactionActive()) {
|
|
||||||
throw TransactionRequiredException::transactionRequired();
|
|
||||||
}
|
|
||||||
|
|
||||||
return $persister->load($sortedId, null, null, array(), $lockMode);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user