예제 #1
0
 /**
  * Processes an individual parameter value.
  *
  * @param mixed $value
  *
  * @return array
  *
  * @throws ORMInvalidArgumentException
  */
 public function processParameterValue($value)
 {
     if (is_array($value)) {
         foreach ($value as $key => $paramValue) {
             $paramValue = $this->processParameterValue($paramValue);
             $value[$key] = is_array($paramValue) ? reset($paramValue) : $paramValue;
         }
         return $value;
     }
     if (is_object($value) && $this->_em->getMetadataFactory()->hasMetadataFor(ClassUtils::getClass($value))) {
         $value = $this->_em->getUnitOfWork()->getSingleIdentifierValue($value);
         if ($value === null) {
             throw ORMInvalidArgumentException::invalidIdentifierBindingEntity();
         }
     }
     return $value;
 }
예제 #2
0
 /**
  * Finds an Entity by its identifier.
  *
  * @param string       $entityName  The class name of the entity to find.
  * @param mixed        $id          The identity of the entity to find.
  * @param integer|null $lockMode    One of the \Doctrine\DBAL\LockMode::* constants
  *                                  or NULL if no specific lock mode should be used
  *                                  during the search.
  * @param integer|null $lockVersion The version of the entity to find when using
  *                                  optimistic locking.
  *
  * @return object|null The entity instance or NULL if the entity can not be found.
  *
  * @throws OptimisticLockException
  * @throws ORMInvalidArgumentException
  * @throws TransactionRequiredException
  * @throws ORMException
  */
 public function find($entityName, $id, $lockMode = null, $lockVersion = null)
 {
     $class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\'));
     if (is_object($id) && $this->metadataFactory->hasMetadataFor(ClassUtils::getClass($id))) {
         $id = $this->unitOfWork->getSingleIdentifierValue($id);
         if ($id === null) {
             throw ORMInvalidArgumentException::invalidIdentifierBindingEntity();
         }
     }
     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 (true) {
             case LockMode::OPTIMISTIC === $lockMode:
                 $this->lock($entity, $lockMode, $lockVersion);
                 break;
             case LockMode::NONE === $lockMode:
             case LockMode::PESSIMISTIC_READ === $lockMode:
             case LockMode::PESSIMISTIC_WRITE === $lockMode:
                 $persister = $unitOfWork->getEntityPersister($class->name);
                 $persister->refresh($sortedId, $entity, $lockMode);
                 break;
         }
         return $entity;
         // Hit!
     }
     $persister = $unitOfWork->getEntityPersister($class->name);
     switch (true) {
         case LockMode::OPTIMISTIC === $lockMode:
             if (!$class->isVersioned) {
                 throw OptimisticLockException::notVersioned($class->name);
             }
             $entity = $persister->load($sortedId);
             $unitOfWork->lock($entity, $lockMode, $lockVersion);
             return $entity;
         case LockMode::NONE === $lockMode:
         case LockMode::PESSIMISTIC_READ === $lockMode:
         case LockMode::PESSIMISTIC_WRITE === $lockMode:
             if (!$this->getConnection()->isTransactionActive()) {
                 throw TransactionRequiredException::transactionRequired();
             }
             return $persister->load($sortedId, null, null, array(), $lockMode);
         default:
             return $persister->loadById($sortedId);
     }
 }
예제 #3
0
 /**
  * @param \Doctrine\ORM\Mapping\ClassMetadata $metadata   The entity metadata.
  * @param mixed                               $identifier The entity identifier.
  *
  * @return array
  */
 private function toIdentifierArray(ClassMetadata $metadata, $identifier)
 {
     if (is_object($identifier) && $this->em->getMetadataFactory()->hasMetadataFor(ClassUtils::getClass($identifier))) {
         $identifier = $this->uow->getSingleIdentifierValue($identifier);
         if ($identifier === null) {
             throw ORMInvalidArgumentException::invalidIdentifierBindingEntity();
         }
     }
     return array($metadata->identifier[0] => $identifier);
 }