one() public méthode

Executes the query and returns a single row of result.
public one ( Connection $db = null ) : array | boolean
$db Connection the database connection used to execute the query. If this parameter is not given, the `elasticsearch` application component will be used.
Résultat array | boolean the first row (in terms of an array) of the query result. False is returned if the query results in nothing.
Exemple #1
0
 public function testOne()
 {
     $query = new Query();
     $query->from('yiitest', 'user');
     $result = $query->one($this->getConnection());
     $this->assertEquals(3, count($result['_source']));
     $this->assertArrayHasKey('status', $result['_source']);
     $this->assertArrayHasKey('email', $result['_source']);
     $this->assertArrayHasKey('name', $result['_source']);
     $this->assertArrayHasKey('_id', $result);
     $result = $query->where(['name' => 'user1'])->one($this->getConnection());
     $this->assertEquals(3, count($result['_source']));
     $this->assertArrayHasKey('status', $result['_source']);
     $this->assertArrayHasKey('email', $result['_source']);
     $this->assertArrayHasKey('name', $result['_source']);
     $this->assertArrayHasKey('_id', $result);
     $this->assertEquals(1, $result['_id']);
     $result = $query->where(['name' => 'user5'])->one($this->getConnection());
     $this->assertFalse($result);
 }
Exemple #2
0
 /**
  * Executes query and returns a single row of result.
  * @param Connection $db 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 ActiveRecord $modelClass */
         //            $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 ActiveRecord $class */
         $class = $this->modelClass;
         $model = $class::instantiate($result);
         $class::populateRecord($model, $result);
         if (!empty($this->with)) {
             $models = [$model];
             $this->findWith($this->with, $models);
             $model = $models[0];
         }
         $model->afterFind();
         return $model;
     }
 }