#19789·yii2

Count query with group in class extends \yii\db\Query

Author: sallfrisCreated Mar 17, 2023Updated May 30, 2026
Labelstype:feature

What steps will reproduce the problem?

  1. Create class QueryMyDb extends \yii\db\Query
  2. Modify function in QueryModified
php
    public function createCommand($db = null)
    {
        if ($db === null) {
            $db = Yii::$app->get('myDb');
        }
        return parent::createCommand($db);
    }
  1. Execute request
php
(new QueryMyDb())->from('table')
            ->groupBy('column')
            ->count();

What is the expected result?

Query to myDb

What do you get instead?

Query to db

Additional info

Problem this If use (new static()), the query will be correct

Q A
Yii version 2.0.47
PHP version 7.2
Operating system unix