Exemplo n.º 1
0
 /**
  * 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);
 }