yii\db\Query::addSelect PHP Method

addSelect() public method

Note, that if [[select]] has not been specified before, you should include * explicitly if you want to select all remaining columns too: php $query->addSelect(["*", "CONCAT(first_name, ' ', last_name) AS full_name"])->one();
See also: select()
public addSelect ( string | array | yii\db\Expression $columns )
$columns string | array | yii\db\Expression the columns to add to the select. See [[select()]] for more details about the format of this parameter.
    public function addSelect($columns)
    {
        if ($columns instanceof Expression) {
            $columns = [$columns];
        } elseif (!is_array($columns)) {
            $columns = preg_split('/\\s*,\\s*/', trim($columns), -1, PREG_SPLIT_NO_EMPTY);
        }
        if ($this->select === null) {
            $this->select = $columns;
        } else {
            $this->select = array_merge($this->select, $columns);
        }
        return $this;
    }

Usage Example

Example #1
0
 public function actionIndex()
 {
     $request = Yii::$app->request;
     $menuId = 31;
     $theadArray = QueryField::find()->where(['menuId' => $menuId])->asArray()->with('queryTable')->all();
     $tables = QueryTable::find()->where(['menuId' => $menuId])->asArray()->all();
     $masterTable = $this->getMasterTable($tables);
     if (!$masterTable) {
         $NullPages = new Pagination(['pageParam' => 'pageCurrent', 'pageSizeParam' => 'pageSize', 'totalCount' => 0, 'defaultPageSize' => 20]);
         return $this->render('index', ['models' => [], 'pages' => $NullPages, 'theadArray' => []]);
     }
     $query = new Query();
     $query->from($masterTable['tabName']);
     $query->select($masterTable['tabName'] . '.' . 'id');
     foreach ($tables as $table) {
         if ($table['isMain'] != '1') {
             $query->leftJoin($table['tabName'], $table['condition']);
         }
     }
     //排序字段
     $attributes = [];
     //查询条件
     $where = [];
     foreach ($theadArray as $thead) {
         if ($thead['queryTable']['reName']) {
             $addSelect = $thead['queryTable']['reName'];
         } else {
             $addSelect = $thead['queryTable']['tabName'];
         }
         $addSelect = $addSelect . '.' . $thead['fieldName'];
         if ($thead['makeTbName'] != 1) {
             $addSelect = $thead['fieldName'];
         }
         if ($thead['reName']) {
             //组装排序字段
             array_push($attributes, $thead['reName']);
             //查询字段
             $addSelect = $addSelect . ' ' . 'as' . ' ' . $thead['reName'];
         } else {
             array_push($attributes, $thead['fieldName']);
         }
         $query->addSelect($addSelect);
         //组装查询条件
         if ($thead['isQuery'] == '1' && $thead['reName']) {
             $where[$thead['reName']] = $request->get($thead['reName']);
         } elseif ($thead['isQuery'] == '1') {
             $where[$thead['fieldName']] = $request->get($thead['fieldName']);
         }
     }
     $query->where($where);
     $pages = new Pagination(['pageParam' => 'pageCurrent', 'pageSizeParam' => 'pageSize', 'defaultPageSize' => 20]);
     $sort = new WetSort(['attributes' => $attributes]);
     $provider = new ActiveDataProvider(['query' => $query, 'pagination' => $pages, 'sort' => $sort]);
     $models = $provider->getModels();
     return $this->render('index', ['models' => $models, 'pages' => $pages, 'theadArray' => $theadArray]);
 }
All Usage Examples Of yii\db\Query::addSelect