/** * 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()); } $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; }
/** * Prepare the Cursor returned by {@link Query::execute()}. * * This method will wrap the base Cursor with an ODM Cursor or EagerCursor, * and set the hydrate option and UnitOfWork hints. This occurs in addition * to any preparation done by the base Query class. * * @see \Doctrine\MongoDB\Cursor::prepareCursor() * @param BaseCursor $cursor * @return Cursor|EagerCursor */ protected function prepareCursor(BaseCursor $cursor) { $cursor = parent::prepareCursor($cursor); // Unwrap a base EagerCursor if ($cursor instanceof BaseEagerCursor) { $cursor = $cursor->getCursor(); } // Convert the base Cursor into an ODM Cursor $cursor = new Cursor($cursor, $this->dm->getUnitOfWork(), $this->class); // Wrap ODM Cursor with EagerCursor if (!empty($this->query['eagerCursor'])) { $cursor = new EagerCursor($cursor, $this->dm->getUnitOfWork(), $this->class); } $cursor->hydrate($this->hydrate); $cursor->setHints($this->unitOfWorkHints); return $cursor; }