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

INTERNAL: Tries to get a document by its identifier hash. If no document is found for the given hash, FALSE is returned.
public tryGetById ( mixed $id, ClassMetadata $class ) : mixed
$id mixed Document identifier
$class Doctrine\ODM\MongoDB\Mapping\ClassMetadata Document class
Результат mixed The found document or FALSE.
 /**
  * Finds a document by its identifier
  *
  * @throws LockException
  * @param string|object $id The identifier
  * @param int $lockMode
  * @param int $lockVersion
  * @return object The document.
  */
 public function find($id, $lockMode = LockMode::NONE, $lockVersion = null)
 {
     if ($id === null) {
         return;
     }
     if (is_array($id)) {
         list($identifierFieldName) = $this->class->getIdentifierFieldNames();
         if (!isset($id[$identifierFieldName])) {
             throw MongoDBException::missingIdentifierField($this->documentName, $identifierFieldName);
         }
         $id = $id[$identifierFieldName];
     }
     // Check identity map first
     if ($document = $this->uow->tryGetById($id, $this->class->rootDocumentName)) {
         if ($lockMode != LockMode::NONE) {
             $this->dm->lock($document, $lockMode, $lockVersion);
         }
         return $document;
         // Hit!
     }
     if ($lockMode == LockMode::NONE) {
         return $this->uow->getDocumentPersister($this->documentName)->load($id);
     } else {
         if ($lockMode == LockMode::OPTIMISTIC) {
             if (!$this->class->isVersioned) {
                 throw LockException::notVersioned($this->documentName);
             }
             $document = $this->uow->getDocumentPersister($this->documentName)->load($id);
             $this->uow->lock($document, $lockMode, $lockVersion);
             return $document;
         } else {
             return $this->uow->getDocumentPersister($this->documentName)->load($id, null, array(), $lockMode);
         }
     }
 }
 /**
  * Finds a document by its identifier.
  *
  * @param $id The identifier.
  * @param int $lockMode
  * @param int $lockVersion
  * @return object The document.
  */
 public function find($id, $lockMode = LockMode::NONE, $lockVersion = null)
 {
     // Check identity map first
     if ($document = $this->uow->tryGetById($id, $this->class->rootDocumentName)) {
         if ($lockMode != LockMode::NONE) {
             $this->dm->lock($document, $lockMode, $lockVersion);
         }
         return $document;
         // Hit!
     }
     $id = array('_id' => $id);
     if ($lockMode == LockMode::NONE) {
         return $this->uow->getDocumentPersister($this->documentName)->load($id);
     } else {
         if ($lockMode == LockMode::OPTIMISTIC) {
             if (!$this->class->isVersioned) {
                 throw LockException::notVersioned($this->documentName);
             }
             $document = $this->uow->getDocumentPersister($this->documentName)->load($id);
             $this->uow->lock($document, $lockMode, $lockVersion);
             return $document;
         } else {
             return $this->uow->getDocumentPersister($this->documentName)->load($id, null, array(), $lockMode);
         }
     }
 }
Пример #3
0
 /**
  * Prime a collection of documents property with an efficient single query instead of
  * lazily loading each field with a single query.
  *
  * @param Iterator $collection
  * @param string $fieldName
  * @param Closure|boolean $primer
  * @param array $hints
  */
 public function primeCollection(Iterator $collection, $fieldName, $primer, array $hints = array())
 {
     $collection = $collection->toArray();
     if (!count($collection)) {
         return;
     }
     $collectionMetaData = $this->dm->getClassMetaData(get_class(current($collection)));
     $fieldMapping = $collectionMetaData->fieldMappings[$fieldName];
     $cmd = $this->cmd;
     $groupedIds = array();
     foreach ($collection as $element) {
         if ($fieldMapping['type'] == 'many') {
             $fieldValue = $collectionMetaData->getFieldValue($element, $fieldName);
             if ($fieldValue instanceof PersistentCollection) {
                 foreach ($fieldValue->getMongoData() as $key => $reference) {
                     if (isset($fieldMapping['simple']) && $fieldMapping['simple']) {
                         $className = $fieldMapping['targetDocument'];
                         $mongoId = $reference;
                     } else {
                         $className = $this->dm->getClassNameFromDiscriminatorValue($fieldMapping, $reference);
                         $mongoId = $reference[$cmd . 'id'];
                     }
                     $id = (string) $mongoId;
                     $document = $this->uow->tryGetById($id, $className);
                     if (!$document || $document instanceof Proxy && !$document->__isInitialized__) {
                         if (!isset($groupedIds[$className])) {
                             $groupedIds[$className] = array();
                         }
                         $groupedIds[$className][] = $mongoId;
                     }
                 }
             }
         } else {
             if ($fieldMapping['type'] == 'one') {
                 $document = $collectionMetaData->getFieldValue($element, $fieldName);
                 if ($document && $document instanceof Proxy && !$document->__isInitialized__) {
                     $class = $this->dm->getClassMetadata(get_class($document));
                     $groupedIds[$class->name][] = $this->uow->getDocumentIdentifier($document);
                 }
             }
         }
     }
     foreach ($groupedIds as $className => $ids) {
         $class = $this->dm->getClassMetadata($className);
         if ($primer instanceof \Closure) {
             $primer($this->dm, $className, $fieldName, $ids, $hints);
         } else {
             $repository = $this->dm->getRepository($className);
             $qb = $repository->createQueryBuilder()->field($class->identifier)->in($ids);
             if (isset($hints[Query::HINT_SLAVE_OKAY])) {
                 $qb->slaveOkay(true);
             }
             $query = $qb->getQuery();
             $query->execute()->toArray();
         }
     }
 }
Пример #4
0
 /**
  * Gets a partial reference to the document identified by the given type and identifier
  * without actually loading it, if the document is not yet loaded.
  *
  * The returned reference may be a partial object if the document is not yet loaded/managed.
  * If it is a partial object it will not initialize the rest of the document state on access.
  * Thus you can only ever safely access the identifier of an document obtained through
  * this method.
  *
  * The use-cases for partial references involve maintaining bidirectional associations
  * without loading one side of the association or to update an document without loading it.
  * Note, however, that in the latter case the original (persistent) document data will
  * never be visible to the application (especially not event listeners) as it will
  * never be loaded in the first place.
  *
  * @param string $documentName The name of the document type.
  * @param mixed $identifier The document identifier.
  * @return object The (partial) document reference.
  */
 public function getPartialReference($documentName, $identifier)
 {
     $class = $this->metadataFactory->getMetadataFor($documentName);
     // Check identity map first, if its already in there just return it.
     if ($document = $this->unitOfWork->tryGetById($identifier, $class->rootDocumentName)) {
         return $document;
     }
     $document = $class->newInstance();
     $class->setIdentifierValue($document, $identifier);
     $this->unitOfWork->registerManaged($document, $identifier, array());
     return $document;
 }
 /**
  * Adds identifiers from a PersistentCollection to $groupedIds.
  *
  * If the relation contains simple references, the mapping is assumed to
  * have a target document class defined. Without that, there is no way to
  * infer the class of the referenced documents.
  *
  * @param PersistentCollection $persistentCollection
  * @param array                $groupedIds
  */
 private function addManyReferences(PersistentCollection $persistentCollection, array &$groupedIds)
 {
     $mapping = $persistentCollection->getMapping();
     if (!empty($mapping['simple'])) {
         $className = $mapping['targetDocument'];
         $class = $this->dm->getClassMetadata($className);
     }
     foreach ($persistentCollection->getMongoData() as $reference) {
         if (!empty($mapping['simple'])) {
             $id = $reference;
         } else {
             $id = $reference['$id'];
             $className = $this->uow->getClassNameForAssociation($mapping, $reference);
             $class = $this->dm->getClassMetadata($className);
         }
         $document = $this->uow->tryGetById($id, $class);
         if (!$document || $document instanceof Proxy && !$document->__isInitialized()) {
             $id = $class->getPHPIdentifierValue($id);
             $groupedIds[$className][serialize($id)] = $id;
         }
     }
 }
Пример #6
0
 /**
  * Finds a document by its identifier
  *
  * @param string|object $id The identifier
  * @param int $lockMode
  * @param int $lockVersion
  * @throws Mapping\MappingException
  * @throws LockException
  * @return object The document.
  */
 public function find($id, $lockMode = LockMode::NONE, $lockVersion = null)
 {
     if ($id === null) {
         return;
     }
     /* TODO: What if the ID object has a field with the same name as the
      * class' mapped identifier field name?
      */
     if (is_array($id)) {
         list($identifierFieldName) = $this->class->getIdentifierFieldNames();
         if (isset($id[$identifierFieldName])) {
             $id = $id[$identifierFieldName];
         }
     }
     // Check identity map first
     if ($document = $this->uow->tryGetById($id, $this->class)) {
         if ($lockMode != LockMode::NONE) {
             $this->dm->lock($document, $lockMode, $lockVersion);
         }
         return $document;
         // Hit!
     }
     $criteria = array('_id' => $id);
     if ($lockMode == LockMode::NONE) {
         return $this->getDocumentPersister()->load($criteria);
     }
     if ($lockMode == LockMode::OPTIMISTIC) {
         if (!$this->class->isVersioned) {
             throw LockException::notVersioned($this->documentName);
         }
         if ($document = $this->getDocumentPersister()->load($criteria)) {
             $this->uow->lock($document, $lockMode, $lockVersion);
         }
         return $document;
     }
     return $this->getDocumentPersister()->load($criteria, null, array(), $lockMode);
 }