Doctrine\ODM\MongoDB\Persisters\DocumentPersister::load PHP Метод

load() публичный Метод

Loads an document by a list of field criteria.
public load ( array $criteria, object $document = null, array $hints = [], integer $lockMode ) : object
$criteria array The criteria by which to load the document.
$document object The document to load the data into. If not specified, a new document is created.
$hints array Hints for document creation.
$lockMode integer
Результат object The loaded and managed document instance or NULL if the document can not be found.
    public function load($criteria, $document = null, array $hints = array(), $lockMode = 0)
    {
        $criteria = $this->prepareQuery($criteria);
        $result = $this->collection->findOne($criteria);

        if ($this->class->isLockable) {
            $lockMapping = $this->class->fieldMappings[$this->class->lockField];
            if (isset($result[$lockMapping['name']]) && $result[$lockMapping['name']] === LockMode::PESSIMISTIC_WRITE) {
                throw LockException::lockFailed($result);
            }
        }

        return $this->createDocument($result, $document, $hints);
    }

Usage Example

Пример #1
0
 /**
  * Generates a closure capable of finalizing a cloned proxy
  *
  * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $classMetadata
  * @param \Doctrine\ODM\MongoDB\Persisters\DocumentPersister $documentPersister
  * @param \ReflectionProperty                                $reflectionId
  *
  * @return \Closure
  *
  * @throws \Doctrine\ODM\MongoDB\DocumentNotFoundException
  */
 private function createCloner(BaseClassMetadata $classMetadata, DocumentPersister $documentPersister, ReflectionProperty $reflectionId)
 {
     return function (BaseProxy $proxy) use($documentPersister, $classMetadata, $reflectionId) {
         if ($proxy->__isInitialized()) {
             return;
         }
         $proxy->__setInitialized(true);
         $proxy->__setInitializer(null);
         $id = $reflectionId->getValue($proxy);
         $original = $documentPersister->load(array('_id' => $id));
         if (null === $original) {
             throw DocumentNotFoundException::documentNotFound(get_class($proxy), $id);
         }
         foreach ($classMetadata->getReflectionClass()->getProperties() as $reflectionProperty) {
             $propertyName = $reflectionProperty->getName();
             if ($classMetadata->hasField($propertyName) || $classMetadata->hasAssociation($propertyName)) {
                 $reflectionProperty->setAccessible(true);
                 $reflectionProperty->setValue($proxy, $reflectionProperty->getValue($original));
             }
         }
     };
 }