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

addGroupBy() public méthode

Adds additional group-by columns to the existing ones.
See also: groupBy()
public addGroupBy ( string | array $columns )
$columns string | array additional 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 addGroupBy($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->groupBy === null) {
            $this->groupBy = $columns;
        } else {
            $this->groupBy = array_merge($this->groupBy, $columns);
        }
        return $this;
    }

Usage Example

 public function testGroup()
 {
     $query = new Query();
     $query->groupBy('team');
     $this->assertEquals(['team'], $query->groupBy);
     $query->addGroupBy('company');
     $this->assertEquals(['team', 'company'], $query->groupBy);
     $query->addGroupBy('age');
     $this->assertEquals(['team', 'company', 'age'], $query->groupBy);
 }