Beispiel #1
0
 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);
             }
         }
     }
 }
Beispiel #2
0
 /**
  * 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;
         }
     }
 }