Gets the collection for this model
public getCollection ( ) : MongoCollection | ||
return | MongoCollection |
/** * The cursor constructor * @param string|EMongoDocument $modelClass - The class name for the active record * @param array|MongoCursor|EMongoCriteria $criteria - Either a condition array (without sort,limit and skip) or a MongoCursor Object * @param array $fields */ public function __construct($modelClass, $criteria = array(), $fields = array()) { // If $fields has something in it if (!empty($fields)) { $this->partial = true; } if (is_string($modelClass)) { $this->modelClass = $modelClass; $this->model = EMongoDocument::model($this->modelClass); } elseif ($modelClass instanceof EMongoDocument) { $this->modelClass = get_class($modelClass); $this->model = $modelClass; } if ($criteria instanceof MongoCursor) { $this->cursor = $criteria; $this->cursor->reset(); } elseif ($criteria instanceof EMongoCriteria) { $this->criteria = $criteria; $this->cursor = $this->model->getCollection()->find($criteria->condition, $criteria->project)->sort($criteria->sort); if ($criteria->skip > 0) { $this->cursor->skip($criteria->skip); } if ($criteria->limit > 0) { $this->cursor->limit($criteria->limit); } } else { // Then we are doing an active query $this->criteria = $criteria; $this->cursor = $this->model->getCollection()->find($criteria, $fields); } }
/** * Gets the active record for the current row * @return EMongoDocument|mixed * @throws EMongoException */ public function current() { if (!$this->run) { if ($this->model->getDbConnection()->queryCachingCount > 0 && $this->model->getDbConnection()->queryCachingDuration > 0 && $this->model->getDbConnection()->queryCacheID !== false && ($cache = Yii::app()->getComponent($this->model->getDbConnection()->queryCacheID)) !== null) { $this->model->getDbConnection()->queryCachingCount--; $info = $this->cursor()->info(); $cacheKey = 'yii:dbquery' . $this->model->getDbConnection()->server . ':' . $this->model->getDbConnection()->db . ':' . $this->model->getDbConnection()->getSerialisedQuery(is_array($info['query']) && isset($info['query']['$query']) ? $info['query']['$query'] : array(), $info['fields'], is_array($info['query']) && isset($info['query']['$orderby']) ? $info['query']['$orderby'] : array(), $info['skip'], $info['limit']) . ':' . $this->model->getCollection(); if (($result = $cache->get($cacheKey)) !== false) { Yii::trace('Query result found in cache', 'extensions.MongoYii.EMongoDocument'); $this->cachedArray = $result; $this->fromCache = true; } else { $this->cachedArray = iterator_to_array($this->cursor); } } if (isset($cache, $cacheKey)) { $cache->set($cacheKey, $this->cachedArray, $this->model->getDbConnection()->queryCachingDuration, $this->model->getDbConnection()->queryCachingDependency); $this->fromCache = true; } $this->run = true; } if ($this->model === null) { throw new EMongoException(Yii::t('yii', 'The MongoCursor must have a model')); } if ($this->fromCache) { return $this->current = $this->model->populateRecord(current($this->cachedArray), true, $this->partial); } return $this->current = $this->model->populateRecord($this->cursor()->current(), true, $this->partial); }
/** * Counts the records returned by the criteria. By default this will not take skip and limit into account * you can add inject true as the first and only parameter to enable MongoDB to take those offsets into * consideration. * * @param bool $takeSkip * @return int */ public function count($takeSkip = false) { return $this->model->getCollection()->count($this->query, $this->options); }