示例#1
0
 /**
  * Finds an entity by its identifier.
  *
  * @param $id The identifier.
  * @param int $lockMode
  * @param int $lockVersion
  * @return object The entity.
  */
 public function find($id, $lockMode = LockMode::NONE, $lockVersion = null)
 {
     // Check identity map first
     if ($entity = $this->xem->getUnitOfWork()->tryGetById($id, $this->class->rootXmlEntityName)) {
         if ($lockMode != LockMode::NONE) {
             $this->xem->lock($entity, $lockMode, $lockVersion);
         }
         return $entity;
         // Hit!
     }
     if ($lockMode == LockMode::NONE) {
         return $this->xem->getUnitOfWork()->getXmlEntityPersister($this->entityName)->load($id);
     } else {
         if ($lockMode == LockMode::OPTIMISTIC) {
             if (!$this->class->isVersioned) {
                 throw OptimisticLockException::notVersioned($this->entityName);
             }
             $entity = $this->xem->getUnitOfWork()->getEntityPersister($this->entityName)->load($id);
             $this->xem->getUnitOfWork()->lock($entity, $lockMode, $lockVersion);
             return $entity;
         } else {
             if (!$this->xem->getConnection()->isTransactionActive()) {
                 throw TransactionRequiredException::transactionRequired();
             }
             return $this->xem->getUnitOfWork()->getXmlEntityPersister($this->entityName)->load($id, null, null, array(), $lockMode);
         }
     }
 }