diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index c91e497cf..50e73824c 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -354,21 +354,82 @@ class EntityManager implements ObjectManager $this->unitOfWork->commit($entity); } - + /** * Finds an Entity by its identifier. * - * This is just a convenient shortcut for getRepository($entityName)->find($id). - * * @param string $entityName - * @param mixed $identifier - * @param int $lockMode - * @param int $lockVersion + * @param mixed $id + * @param integer $lockMode + * @param integer $lockVersion + * * @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); + } } /** diff --git a/lib/Doctrine/ORM/EntityRepository.php b/lib/Doctrine/ORM/EntityRepository.php index 72ae8c589..2dbf37a50 100644 --- a/lib/Doctrine/ORM/EntityRepository.php +++ b/lib/Doctrine/ORM/EntityRepository.php @@ -125,71 +125,15 @@ class EntityRepository implements ObjectRepository, Selectable /** * Finds an entity by its primary key / identifier. * - * @param $id The identifier. - * @param int $lockMode - * @param int $lockVersion + * @param mixed $id The identifier. + * @param integer $lockMode + * @param integer $lockVersion + * * @return object The entity. */ public function find($id, $lockMode = LockMode::NONE, $lockVersion = null) { - if ( ! is_array($id)) { - $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); - } + return $this->_em->find($this->_entityName, $id, $lockMode, $lockVersion); } /**