getReference() public method

If partial objects are allowed, this method will return a partial object that only has its identifier populated. Otherwise a proxy is returned that automatically loads itself on first access.
public getReference ( string $documentName, string | object $identifier ) : mixed | object
$documentName string
$identifier string | object
return mixed | object The document reference.
 private function loadReferenceManyCollectionOwningSide(PersistentCollection $collection)
 {
     $mapping = $collection->getMapping();
     $groupedIds = array();
     $sorted = isset($mapping['sort']) && $mapping['sort'];
     foreach ($collection->getMongoData() as $key => $reference) {
         if (isset($mapping['simple']) && $mapping['simple']) {
             $className = $mapping['targetDocument'];
             $mongoId = $reference;
         } else {
             $className = $this->uow->getClassNameForAssociation($mapping, $reference);
             $mongoId = $reference['$id'];
         }
         $id = $this->dm->getClassMetadata($className)->getPHPIdentifierValue($mongoId);
         $reference = $this->dm->getReference($className, $id);
         /* If the reference has custom sort, remember IDs to execute the query later.
          * Otherwise add the references right now in the order they are embedded.
          */
         if ($sorted) {
             $groupedIds[$className][] = $mongoId;
         } else {
             if ($mapping['strategy'] === 'set') {
                 $collection->set($key, $reference);
             } else {
                 $collection->add($reference);
             }
         }
     }
     if ($sorted && count($groupedIds)) {
         $this->loadActualDataForSortedReferenceManyCollectionByIds($collection, $groupedIds);
     }
 }
 private function loadReferenceManyCollection(PersistentCollection $collection)
 {
     $mapping = $collection->getMapping();
     $cmd = $this->cmd;
     $groupedIds = array();
     foreach ($collection->getMongoData() as $reference) {
         $className = $this->dm->getClassNameFromDiscriminatorValue($mapping, $reference);
         $mongoId = $reference[$cmd . 'id'];
         $id = (string) $mongoId;
         $reference = $this->dm->getReference($className, $id);
         $collection->add($reference);
         if ($reference instanceof Proxy && !$reference->__isInitialized__) {
             if (!isset($groupedIds[$className])) {
                 $groupedIds[$className] = array();
             }
             $groupedIds[$className][] = $mongoId;
         }
     }
     foreach ($groupedIds as $className => $ids) {
         $class = $this->dm->getClassMetadata($className);
         $mongoCollection = $this->dm->getDocumentCollection($className);
         $data = $mongoCollection->find(array('_id' => array($cmd . 'in' => $ids)));
         foreach ($data as $documentData) {
             $document = $this->uow->getById((string) $documentData['_id'], $class->rootDocumentName);
             $data = $this->hydratorFactory->hydrate($document, $documentData);
             $this->uow->setOriginalDocumentData($document, $data);
         }
     }
 }
    public function loadCollection(PersistentCollection $collection)
    {
        $mapping = $collection->getMapping();
        $cmd = $this->dm->getConfiguration()->getMongoCmd();
        $groupedIds = array();
        foreach ($collection->getReferences() as $reference) {
            $className = $this->dm->getClassNameFromDiscriminatorValue($mapping, $reference);
            $id = $reference[$cmd . 'id'];
            $reference = $this->dm->getReference($className, (string) $id);
            $collection->add($reference);
            if ($reference instanceof Proxy && ! $reference->__isInitialized__) {
                if ( ! isset($groupedIds[$className])) {
                    $groupedIds[$className] = array();
                }
                $groupedIds[$className][] = $id;
            }
        }

        foreach ($groupedIds as $className => $ids) {
            $mongoCollection = $this->dm->getDocumentCollection($className);
            $data = $mongoCollection->find(array('_id' => array($cmd . 'in' => $ids)));
            $hints = array(Builder::HINT_REFRESH => true);
            foreach ($data as $id => $documentData) {
                $document = $this->uow->getOrCreateDocument($className, $documentData, $hints);
            }
        }
    }
Beispiel #4
0
 private function loadReferenceManyCollectionOwningSide(PersistentCollection $collection)
 {
     $hints = $collection->getHints();
     $mapping = $collection->getMapping();
     $cmd = $this->cmd;
     $groupedIds = array();
     foreach ($collection->getMongoData() as $key => $reference) {
         if (isset($mapping['simple']) && $mapping['simple']) {
             $className = $mapping['targetDocument'];
             $mongoId = $reference;
         } else {
             $className = $this->dm->getClassNameFromDiscriminatorValue($mapping, $reference);
             $mongoId = $reference[$cmd . 'id'];
         }
         $id = (string) $mongoId;
         if (!$id) {
             continue;
         }
         $reference = $this->dm->getReference($className, $id);
         if ($mapping['strategy'] === 'set') {
             $collection->set($key, $reference);
         } else {
             $collection->add($reference);
         }
         if ($reference instanceof Proxy && !$reference->__isInitialized__) {
             if (!isset($groupedIds[$className])) {
                 $groupedIds[$className] = array();
             }
             $groupedIds[$className][] = $mongoId;
         }
     }
     foreach ($groupedIds as $className => $ids) {
         $class = $this->dm->getClassMetadata($className);
         $mongoCollection = $this->dm->getDocumentCollection($className);
         $criteria = array_merge(array('_id' => array($cmd . 'in' => $ids)), $this->dm->getFilterCollection()->getFilterCriteria($class), isset($mapping['criteria']) ? $mapping['criteria'] : array());
         $cursor = $mongoCollection->find($criteria);
         if (isset($mapping['sort'])) {
             $cursor->sort($mapping['sort']);
         }
         if (isset($mapping['limit'])) {
             $cursor->limit($mapping['limit']);
         }
         if (isset($mapping['skip'])) {
             $cursor->skip($mapping['skip']);
         }
         if (isset($hints[Query::HINT_SLAVE_OKAY])) {
             $cursor->slaveOkay(true);
         }
         $documents = $cursor->toArray();
         foreach ($documents as $documentData) {
             $document = $this->uow->getById((string) $documentData['_id'], $class->rootDocumentName);
             $data = $this->hydratorFactory->hydrate($document, $documentData);
             $this->uow->setOriginalDocumentData($document, $data);
             $document->__isInitialized__ = true;
         }
     }
 }
 private function initialize()
 {
     if (!$this->initialized) {
         if ($this->isDirty) {
             // Has NEW objects added through add(). Remember them.
             $newObjects = $this->coll->toArray();
         }
         $this->coll->clear();
         $groupedIds = array();
         foreach ($this->references as $reference) {
             $className = $this->dm->getClassNameFromDiscriminatorValue($this->mapping, $reference);
             if (!isset($groupedIds[$className])) {
                 $groupedIds[$className] = array();
             }
             $id = $reference[$this->cmd . 'id'];
             $groupedIds[$className][] = $id;
             $reference = $this->dm->getReference($className, (string) $id);
             $this->add($reference);
         }
         foreach ($groupedIds as $className => $ids) {
             $collection = $this->dm->getDocumentCollection($className);
             $data = $collection->find(array('_id' => array($this->cmd . 'in' => $ids)));
             $hints = array(Query::HINT_REFRESH => Query::HINT_REFRESH);
             foreach ($data as $id => $documentData) {
                 $document = $this->dm->getUnitOfWork()->getOrCreateDocument($className, $documentData, $hints);
                 if ($document instanceof Proxy) {
                     $document->__isInitialized__ = true;
                     unset($document->__dm);
                     unset($document->__identifier);
                 }
             }
         }
         $this->takeSnapshot();
         // Reattach NEW objects added through add(), if any.
         if (isset($newObjects)) {
             foreach ($newObjects as $obj) {
                 $this->coll->add($obj);
             }
             $this->isDirty = true;
         }
         $this->initialized = true;
     }
 }
 private function loadReferenceManyCollectionOwningSide(PersistentCollection $collection)
 {
     $hints = $collection->getHints();
     $mapping = $collection->getMapping();
     $groupedIds = array();
     $sorted = isset($mapping['sort']) && $mapping['sort'];
     foreach ($collection->getMongoData() as $key => $reference) {
         if (isset($mapping['simple']) && $mapping['simple']) {
             $className = $mapping['targetDocument'];
             $mongoId = $reference;
         } else {
             $className = $this->uow->getClassNameForAssociation($mapping, $reference);
             $mongoId = $reference['$id'];
         }
         $id = $this->dm->getClassMetadata($className)->getPHPIdentifierValue($mongoId);
         // create a reference to the class and id
         $reference = $this->dm->getReference($className, $id);
         // no custom sort so add the references right now in the order they are embedded
         if (!$sorted) {
             if (CollectionHelper::isHash($mapping['strategy'])) {
                 $collection->set($key, $reference);
             } else {
                 $collection->add($reference);
             }
         }
         // only query for the referenced object if it is not already initialized or the collection is sorted
         if ($reference instanceof Proxy && !$reference->__isInitialized__ || $sorted) {
             $groupedIds[$className][] = $mongoId;
         }
     }
     foreach ($groupedIds as $className => $ids) {
         $class = $this->dm->getClassMetadata($className);
         $mongoCollection = $this->dm->getDocumentCollection($className);
         $criteria = $this->cm->merge(array('_id' => array('$in' => array_values($ids))), $this->dm->getFilterCollection()->getFilterCriteria($class), isset($mapping['criteria']) ? $mapping['criteria'] : array());
         $criteria = $this->uow->getDocumentPersister($className)->prepareQueryOrNewObj($criteria);
         $cursor = $mongoCollection->find($criteria);
         if (isset($mapping['sort'])) {
             $cursor->sort($mapping['sort']);
         }
         if (isset($mapping['limit'])) {
             $cursor->limit($mapping['limit']);
         }
         if (isset($mapping['skip'])) {
             $cursor->skip($mapping['skip']);
         }
         if (!empty($hints[Query::HINT_SLAVE_OKAY])) {
             $cursor->slaveOkay(true);
         }
         if (!empty($hints[Query::HINT_READ_PREFERENCE])) {
             $cursor->setReadPreference($hints[Query::HINT_READ_PREFERENCE], $hints[Query::HINT_READ_PREFERENCE_TAGS]);
         }
         $documents = $cursor->toArray(false);
         foreach ($documents as $documentData) {
             $document = $this->uow->getById($documentData['_id'], $class);
             $data = $this->hydratorFactory->hydrate($document, $documentData);
             $this->uow->setOriginalDocumentData($document, $data);
             $document->__isInitialized__ = true;
             if ($sorted) {
                 $collection->add($document);
             }
         }
     }
 }