yii\elasticsearch\ActiveQuery::one PHP Method

one() public method

Executes query and returns a single row of result.
public one ( Connection $db = null ) : ActiveRecord | array | null
$db Connection the DB connection used to create the DB command. If null, the DB 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)
    {
        if (($result = parent::one($db)) === false) {
            return null;
        }
        if ($this->asArray) {
            // TODO implement with()
            //            /* @var $modelClass ActiveRecord */
            //            $modelClass = $this->modelClass;
            //            $model = $result['_source'];
            //            $pk = $modelClass::primaryKey()[0];
            //            if ($pk === '_id') {
            //                $model['_id'] = $result['_id'];
            //            }
            //            $model['_score'] = $result['_score'];
            //            if (!empty($this->with)) {
            //                $models = [$model];
            //                $this->findWith($this->with, $models);
            //                $model = $models[0];
            //            }
            return $result;
        } else {
            /* @var $class ActiveRecord */
            $class = $this->modelClass;
            $model = $class::instantiate($result);
            $class = get_class($model);
            $class::populateRecord($model, $result);
            if (!empty($this->with)) {
                $models = [$model];
                $this->findWith($this->with, $models);
                $model = $models[0];
            }
            $model->afterFind();
            return $model;
        }
    }