/**
* 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;
}