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.
Esempio n. 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]);
 }
 public function actionReputation()
 {
     $query = new Query();
     $query2 = new Query();
     $query->addSelect('*')->from('reputation r');
     $fullrep = $query->all();
     $query2->select('id, user_id, rep_id, timestamp')->from('`reputation_history`')->where('user_id = ' . $this->getUser()->getId());
     $myrep = $query2->all();
     $sum = ReputationHistory::getReputationHistorySum($this->contentContainer->getId());
     return $this->render('reputation', ['user' => $this->contentContainer->attributes, 'rep' => $fullrep, 'myrep' => $myrep, 'sum' => $sum]);
 }
Esempio n. 3
0
 public function testSelect()
 {
     // default
     $query = new Query();
     $query->select('*');
     $this->assertEquals(['*'], $query->select);
     $this->assertNull($query->distinct);
     $this->assertEquals(null, $query->selectOption);
     $query = new Query();
     $query->select('id, name', 'something')->distinct(true);
     $this->assertEquals(['id', 'name'], $query->select);
     $this->assertTrue($query->distinct);
     $this->assertEquals('something', $query->selectOption);
     $query = new Query();
     $query->select('id, name');
     $query->addSelect('email');
     $this->assertEquals(['id', 'name', 'email'], $query->select);
 }
Esempio n. 4
0
 function actionAdminList()
 {
     $query = new Query();
     $query->addSelect('c.*')->from([Menu::tableName() . ' c']);
     $provider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 100]]);
     $model = new MenuForm();
     if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
         $object = new Menu();
         $object->name = $model->name;
         $object->url = $model->url;
         $object->sort_int = 0;
         $object->active = 1;
         $object->parent_id = 0;
         $object->save();
         $model->command_reload_page = 1;
         //return $this->redirect(Url::toRoute(['posts/menu-list']));
     }
     $this->layout = 'admin';
     return $this->render('//admin/menu/list.php', ['provider' => $provider, 'model' => $model]);
 }
Esempio n. 5
0
 function run()
 {
     // && $model->contact(Yii::$app->params['adminEmail'])
     $this->enable_form = $this->allow_comments();
     $comment = new Comments();
     $comment->parent_id = 0;
     if ($this->enable_form && $comment->load(Yii::$app->request->post()) && $comment->validate()) {
         Yii::$app->session->setFlash('commentFormSubmitted');
         $comment->active = true;
         $comment->user_id = Yii::$app->user->isGuest ? null : Yii::$app->user->identity->id;
         $comment->ip = ip2long(Yii::$app->request->getUserIP());
         $comment->agent = Yii::$app->request->getUserAgent();
         $comment->saveUploadedImage('file');
         $comment->save();
         return Yii::$app->getResponse()->redirect(Yii::$app->getRequest()->getUrl());
     }
     $query = new Query();
     $query->addSelect('c.*, f.filename, f.thumb_filename, f.size, u.username')->from([Comments::tableName() . ' c'])->leftJoin(User::tableName() . ' u', 'u.id = c.user_id')->leftJoin(Files::tableName() . ' f', 'f.id = c.image_id')->where(['c.active' => true])->andWhere(['c.post_id' => $this->post_id]);
     $comment->post_id = $this->post_id;
     return $this->render('comments/comments_box', ['comments' => $this->buildTree($query->all()), 'model' => $comment, 'enable' => $this->enable_form]);
 }
Esempio n. 6
0
 /**
  * Возвращает отсортированные данные
  * @return mixed
  */
 function findAllAndBuildTree($menu_id)
 {
     $query = new Query();
     $query->addSelect('m.*')->from([$this::tableName() . ' m'])->where(['menu_id' => $menu_id]);
     return $this->buildTree($this::findAll(['menu_id' => $menu_id]));
 }
Esempio n. 7
0
 public static function find($module, $params = [], $page = 30)
 {
     $relation = ['country' => ['translations'], 'city' => ['country', 'country.translations', 'airports', 'airports.translations', 'translations'], 'direction' => ['iata', 'from', 'from.translations', 'from.country', 'from.country.translations', 'to', 'to.translations', 'to.country', 'to.country.translations'], 'airport' => ['translations', 'country', 'country.translations', 'city', 'city.translations'], 'airline' => []];
     if (!empty($params['relation'])) {
         $relation = array_merge($relation, $params['relation']);
         unset($params['relation']);
     }
     $model = 'common\\models\\Received' . ucfirst($module);
     if (!class_exists($model)) {
         throw new ErrorException('no module > ' . $module);
     }
     $query = new Query();
     $query->select(["{$module}.id"])->from([$module => "received_{$module}"]);
     if ($module == 'direction') {
         $query->addSelect(["{$module}.popularity"]);
         $query->orderBy(['direction.popularity' => SORT_DESC]);
         $query->groupBy(['direction.city_to_code', 'direction.city_from_code']);
     }
     foreach ($params as $key => $value) {
         $postfix = '';
         if ($key == 'from' || $key == 'to') {
             $postfix = "_{$key}";
             $key = 'city';
         }
         if (is_array($value) && $value) {
             $query->leftJoin([$key . $postfix => "received_{$key}"], "{$module}.{$key}{$postfix}_code = {$key}{$postfix}.code");
             foreach ($value as $_key => $_value) {
                 if (is_array($_value) && $_value) {
                     $query->leftJoin([$_key . $postfix => "received_{$_key}"], "{$key}{$postfix}.{$_key}_code = {$_key}{$postfix}.code");
                     foreach ($_value as $__key => $__value) {
                         $query->andFilterWhere(["{$_key}{$postfix}.{$__key}" => $__value]);
                     }
                 } else {
                     $query->andFilterWhere(["{$key}{$postfix}.{$_key}" => $_value]);
                 }
             }
         } else {
             $query->andFilterWhere(["{$module}.{$key}{$postfix}" => $value]);
         }
     }
     $total = $query->count();
     $id = $query->column();
     if ($page > 1) {
         if (!empty($_GET['page'])) {
             $_page = $_GET['page'];
         } else {
             $_page = 0;
         }
         $id = array_slice($id, $_page > 1 ? ($_page - 1) * $page : 0, $page);
         //            VarDumper::dump($id);
     }
     /** @var $model \yii\db\ActiveRecord */
     $query = $model::find()->where(['id' => $id])->asArray();
     if ($module == 'direction') {
         $query->orderBy(['received_direction.popularity' => SORT_DESC]);
     }
     $query->with($relation[$module]);
     if ($page == 1) {
         return $query->all();
     }
     $pagination = new Pagination(['totalCount' => $total, 'pageSize' => $page, 'pageSizeParam' => false]);
     if ($page == 1) {
         $page = 0;
     }
     $query->offset(0)->limit($page);
     return [$query->all(), $pagination];
 }