Gets the filter collection.
public getFilterCollection ( ) : Doctrine\ODM\MongoDB\Query\FilterCollection | ||
return | Doctrine\ODM\MongoDB\Query\FilterCollection | The active filter collection. |
/** * Enable filters for an given document manager * * @param DocumentManager $documentManager * * @return null */ private function enableFilters(DocumentManager $documentManager) { if (empty($this->enabledFilters)) { return; } $filterCollection = $documentManager->getFilterCollection(); foreach ($this->enabledFilters as $filter) { $filterCollection->enable($filter); } }
/** * Execute the query and returns the results. * * @return mixed */ public function execute() { $uow = $this->dm->getUnitOfWork(); if ($this->isIndexRequired() && !$this->isIndexed()) { throw MongoDBException::queryNotIndexed($this->class->name, $this->getUnindexedFields()); } $this->query['query'] = array_merge($this->query['query'], $this->dm->getFilterCollection()->getFilterCriteria($this->class)); $results = parent::execute(); $hints = array(); if ($this->refresh) { $hints[self::HINT_REFRESH] = true; } if ($this->query['slaveOkay'] === true) { $hints[self::HINT_SLAVE_OKAY] = true; } // Unwrap the BaseEagerCursor if ($results instanceof BaseEagerCursor) { $results = $results->getCursor(); } // Convert the regular mongodb cursor to the odm cursor if ($results instanceof BaseCursor) { $results = $this->wrapCursor($results, $hints); } // Wrap odm cursor with EagerCursor if true if ($this->query['eagerCursor'] === true) { $results = new EagerCursor($results, $this->dm->getUnitOfWork(), $this->class); } // GeoLocationFindQuery just returns an instance of ArrayIterator so we have to // iterator over it and hydrate each object. if ($this->query['type'] === self::TYPE_GEO_LOCATION && $this->hydrate) { foreach ($results as $key => $result) { $document = $result['obj']; if ($this->class->distance) { $document[$this->class->distance] = $result['dis']; } $results[$key] = $uow->getOrCreateDocument($this->class->name, $document, $hints); } $results->reset(); } if ($this->primers) { $documentPersister = $this->dm->getUnitOfWork()->getDocumentPersister($this->class->name); foreach ($this->primers as $fieldName => $primer) { if ($primer) { $documentPersister->primeCollection($results, $fieldName, $primer, $hints); } } } if ($this->hydrate && is_array($results) && isset($results['_id'])) { // Convert a single document array to a document object $results = $uow->getOrCreateDocument($this->class->name, $results, $hints); } return $results; }
/** * Adds filter criteria to an already-prepared query. * * This method should be used once for query criteria and not be used for * nested expressions. It should be called after * {@link DocumentPerister::addDiscriminatorToPreparedQuery()}. * * @param array $preparedQuery * @return array */ public function addFilterToPreparedQuery(array $preparedQuery) { /* If filter criteria exists for this class, prepare it and merge * over the existing query. * * @todo Consider recursive merging in case the filter criteria and * prepared query both contain top-level $and/$or operators. */ if ($filterCriteria = $this->dm->getFilterCollection()->getFilterCriteria($this->class)) { $preparedQuery = $this->cm->merge($preparedQuery, $this->prepareQueryOrNewObj($filterCriteria)); } return $preparedQuery; }
/** * Prepares a query array by converting the portable Doctrine types to the types mongodb expects. * * @param string|array $query * @return array $newQuery */ public function prepareQuery($query = array()) { if (is_scalar($query) || $query instanceof \MongoId) { $query = array('_id' => $query); } $query = array_merge($query, $this->dm->getFilterCollection()->getFilterCriteria($this->class)); if ($this->class->hasDiscriminator() && !isset($query[$this->class->discriminatorField['name']])) { $discriminatorValues = $this->getClassDiscriminatorValues($this->class); $query[$this->class->discriminatorField['name']] = array('$in' => $discriminatorValues); } $newQuery = array(); if ($query) { foreach ($query as $key => $value) { if (isset($key[0]) && $key[0] === $this->cmd && is_array($value)) { $newQuery[$key] = $this->prepareSubQuery($value); } else { $newQuery[$key] = $this->prepareQueryElement($key, $value, null, true); } } $newQuery = $this->convertTypes($newQuery); } return $newQuery; }
/** * @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); } } }
/** * Configures the Doctrine entity manager instance. * * @param array $doctrineConfig * @param DocumentManager $documentManager */ protected function configureDocumentManager(array $doctrineConfig, DocumentManager $documentManager) { if (isset($doctrineConfig['filters'])) { foreach ($doctrineConfig['filters'] as $name => $filter) { if (!array_get($filter, 'enabled', false)) { continue; } $documentManager->getFilterCollection()->enable($name); } } // @see http://doctrine-mongodb-odm.readthedocs.org/en/latest/reference/basic-mapping.html#custom-mapping-types if (isset($doctrineConfig['types'])) { foreach ($doctrineConfig['types'] as $name => $className) { if (!Type::hasType($name)) { Type::addType($name, $className); } else { Type::overrideType($name, $className); } } } }
public function onKernelRequest() { $filter = $this->dm->getFilterCollection()->enable('state_filter'); $this->setReaderToFilter($filter, $this->reader); }