/** * @param PersistentCollection $collection * * @return CursorInterface */ public function createReferenceManyWithRepositoryMethodCursor(PersistentCollection $collection) { $hints = $collection->getHints(); $mapping = $collection->getMapping(); $repositoryMethod = $mapping['repositoryMethod']; $cursor = $this->dm->getRepository($mapping['targetDocument'])->{$repositoryMethod}($collection->getOwner()); if (!$cursor instanceof CursorInterface) { throw new \BadMethodCallException("Expected repository method {$repositoryMethod} to return a CursorInterface"); } 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]); } return $cursor; }
private function loadReferenceManyCollectionInverseSide(PersistentCollection $collection) { $hints = $collection->getHints(); $mapping = $collection->getMapping(); $owner = $collection->getOwner(); $ownerClass = $this->dm->getClassMetadata(get_class($owner)); $targetClass = $this->dm->getClassMetadata($mapping['targetDocument']); $mappedByMapping = $targetClass->fieldMappings[$mapping['mappedBy']]; $mappedByFieldName = isset($mappedByMapping['simple']) && $mappedByMapping['simple'] ? $mapping['mappedBy'] : $mapping['mappedBy'] . '.id'; $criteria = array_merge(array($mappedByFieldName => $ownerClass->getIdentifierObject($owner)), isset($mapping['criteria']) ? $mapping['criteria'] : array()); $qb = $this->dm->createQueryBuilder($mapping['targetDocument'])->setQueryArray($criteria); if (isset($mapping['sort'])) { $qb->sort($mapping['sort']); } if (isset($mapping['limit'])) { $qb->limit($mapping['limit']); } if (isset($mapping['skip'])) { $qb->skip($mapping['skip']); } if (isset($hints[Query::HINT_SLAVE_OKAY])) { $qb->slaveOkay(true); } $documents = $qb->getQuery()->execute()->toArray(); foreach ($documents as $document) { $collection->add($document); } }
/** * {@inheritdoc} */ public function getHints() { return $this->collection->getHints(); }
/** * @param PersistentCollection $collection * * @return Cursor */ public function createReferenceManyWithRepositoryMethodCursor(PersistentCollection $collection) { $hints = $collection->getHints(); $mapping = $collection->getMapping(); $cursor = $this->dm->getRepository($mapping['targetDocument'])->{$mapping}['repositoryMethod']($collection->getOwner()); 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]); } return $cursor; }
/** * @param PersistentCollection $collection * @param array $groupedIds * * @throws \Doctrine\ODM\MongoDB\MongoDBException */ private function loadActualDataForSortedReferenceManyCollectionByIds(PersistentCollection $collection, array $groupedIds) { $mapping = $collection->getMapping(); $hints = $collection->getHints(); 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) { $docId = $documentData['_id']; $document = $this->uow->getById($docId, $class); $data = $this->hydratorFactory->hydrate($document, $documentData); $this->uow->setOriginalDocumentData($document, $data); $document->__isInitialized__ = true; $collection->add($document); } } }