yii\db\Query::column PHP Method

column() public method

Executes the query and returns the first column of the result.
public column ( Connection $db = null ) : array
$db Connection the database connection used to generate the SQL statement. If this parameter is not given, the `db` application component will be used.
return array the first column of the query result. An empty array is returned if the query results in nothing.
    public function column($db = null)
    {
        if ($this->emulateExecution) {
            return [];
        }
        if ($this->indexBy === null) {
            return $this->createCommand($db)->queryColumn();
        }
        if (is_string($this->indexBy) && is_array($this->select) && count($this->select) === 1) {
            $this->select[] = $this->indexBy;
        }
        $rows = $this->createCommand($db)->queryAll();
        $results = [];
        foreach ($rows as $row) {
            $value = reset($row);
            if ($this->indexBy instanceof \Closure) {
                $results[call_user_func($this->indexBy, $row)] = $value;
            } else {
                $results[$row[$this->indexBy]] = $value;
            }
        }
        return $results;
    }

Usage Example

Example #1
0
 public static function getFollowTagIdsByUid($uid)
 {
     //        $sql = "select tag_id from tag_follow where author_id=:author_id";
     //        $tag_ids = Yii::$app->db->createCommand($sql, [
     //            ':author_id' => $uid
     //        ])->queryColumn();
     //        return $tag_ids;
     $query = new Query();
     $query->select('tag_id')->from('tag_follow')->where(['author_id' => $uid]);
     return $query->column();
 }
All Usage Examples Of yii\db\Query::column