yii\db\Query::groupBy PHP Méthode

groupBy() public méthode

Sets the GROUP BY part of the query.
See also: addGroupBy()
public groupBy ( string | array | yii\db\Expression $columns )
$columns string | array | yii\db\Expression the columns to be grouped by. Columns can be specified in either a string (e.g. "id, name") or an array (e.g. ['id', 'name']). The method will automatically quote the column names unless a column contains some parenthesis (which means the column contains a DB expression). Note that if your group-by is an expression containing commas, you should always use an array to represent the group-by information. Otherwise, the method will not be able to correctly determine the group-by columns. Since version 2.0.7, an [[Expression]] object can be passed to specify the GROUP BY part explicitly in plain SQL.
    public function groupBy($columns)
    {
        if ($columns instanceof Expression) {
            $columns = [$columns];
        } elseif (!is_array($columns)) {
            $columns = preg_split('/\\s*,\\s*/', trim($columns), -1, PREG_SPLIT_NO_EMPTY);
        }
        $this->groupBy = $columns;
        return $this;
    }

Usage Example

Exemple #1
0
 /**
  * Lists all UrQuestions models.
  * @return mixed
  */
 public function actionIndex()
 {
     //топ аптек
     $db = new Query();
     $db->from('ur_questions');
     $db->select(['COUNT(*) AS count', 'ur_l_id', 'ur_l.name']);
     $db->leftJoin('ur_l', "ur_l.id=ur_questions.`ur_l_id`");
     $db->groupBy('ur_l_id');
     $db->orderBy('count DESC');
     $db->limit(5);
     $top = $db->all();
     $query = (new \yii\db\Query())->from('ur_questions');
     $query->select('username,ur_questions.id,ur_questions.created_at,question,ur_l.name,date_ansver');
     $query->leftJoin('ur_l', 'ur_questions.ur_l_id = ur_l.id');
     $query->leftJoin('users', 'ur_questions.user_id = users.id');
     if (\Yii::$app->user->identity->status != 1) {
         // $query->where(['ur_questions.user_id'=>\Yii::$app->user->identity->id]);
     }
     $query->orderBy(['ur_questions.id' => SORT_DESC]);
     $dataProvider = new ActiveDataProvider(['key' => 'id', 'query' => $query->from('ur_questions')]);
     return $this->render('index', ['dataProvider' => $dataProvider, 'top' => $top]);
 }
All Usage Examples Of yii\db\Query::groupBy