Beispiel #1
0
 /**
  * Executes query and returns a single row of result.
  * @param \yii\mongodb\Connection $db the Mongo connection used to execute the query.
  * If null, the Mongo connection returned by [[modelClass]] will be used.
  * @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]],
  * the query result may be either an array or an ActiveRecord object. Null will be returned
  * if the query results in nothing.
  */
 public function one($db = null)
 {
     $row = parent::one($db);
     if ($row !== false) {
         if ($this->asArray) {
             $model = $row;
         } else {
             /** @var ActiveRecord $class */
             $class = $this->modelClass;
             $model = $class::instantiate($row);
             $class::populateRecord($model, $row);
         }
         if (!empty($this->with)) {
             $models = [$model];
             $this->findWith($this->with, $models);
             $model = $models[0];
         }
         if (!$this->asArray) {
             $model->afterFind();
         }
         return $model;
     } else {
         return null;
     }
 }
Beispiel #2
0
 /**
  * Executes query and returns a single row of result.
  * @param \yii\mongodb\Connection $db the Mongo connection used to execute the query.
  * If null, the Mongo connection returned by [[modelClass]] will be used.
  * @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]],
  * the query result may be either an array or an ActiveRecord object. Null will be returned
  * if the query results in nothing.
  */
 public function one($db = null)
 {
     return parent::one($db);
 }
Beispiel #3
0
 /**
  * Executes query and returns a single row of result.
  * @param \yii\mongodb\Connection $db the Mongo connection used to execute the query.
  * If null, the Mongo connection returned by [[modelClass]] will be used.
  * @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]],
  * the query result may be either an array or an ActiveRecord object. Null will be returned
  * if the query results in nothing.
  */
 public function one($db = null)
 {
     $row = parent::one($db);
     if ($row !== false) {
         $models = $this->populate([$row]);
         return reset($models) ?: null;
     } else {
         return null;
     }
 }