column() 공개 메소드

Executes the query and returns the first column of the result.
public column ( string $field, Connection $db = null ) : array
$field string the field to query over
$db Connection the database connection used to execute the query. If this parameter is not given, the `elasticsearch` application component will be used.
리턴 array the first column of the query result. An empty array is returned if the query results in nothing.
예제 #1
0
 /**
  * @inheritdoc
  */
 public function column($field, $db = null)
 {
     if ($field == '_id') {
         $command = $this->createCommand($db);
         $command->queryParts['fields'] = [];
         $command->queryParts['_source'] = false;
         $result = $command->search();
         if (empty($result['hits']['hits'])) {
             return [];
         }
         $column = [];
         foreach ($result['hits']['hits'] as $row) {
             $column[] = $row['_id'];
         }
         return $column;
     }
     return parent::column($field, $db);
 }
예제 #2
0
 public function testColumn()
 {
     $query = new Query();
     $query->from('yiitest', 'user');
     $result = $query->orderBy(['name' => SORT_ASC])->column('name', $this->getConnection());
     $this->assertEquals(['user1', 'user2', 'user3', 'user4'], $result);
     $result = $query->column('noname', $this->getConnection());
     $this->assertEquals([null, null, null, null], $result);
     $result = $query->where(['name' => 'user5'])->scalar('name', $this->getConnection());
     $this->assertNull($result);
 }