Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sallfris opened this issue Mar 17, 2023 · 9 comments
Open

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

sallfris opened this issue Mar 17, 2023 · 9 comments

Comments

@sallfris
Copy link

sallfris commented Mar 17, 2023

What steps will reproduce the problem?

  1. Create class QueryMyDb extends \yii\db\Query
  2. Modify function in QueryModified
    public function createCommand($db = null)
    {
        if ($db === null) {
            $db = Yii::$app->get('myDb');
        }
        return parent::createCommand($db);
    }
  1. Execute request
(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
@lubosdz
Copy link
Contributor

lubosdz commented Mar 17, 2023

What's your ../config/main.php section for databases ?

@kamarton
Copy link
Contributor

What's your ../config/main.php section for databases ?

The configuration is not relevant. The problem is that Query#queryScalar() create a instance with new self(), ignoring the request initiated from the modified class.

@lubosdz
Copy link
Contributor

lubosdz commented Mar 18, 2023

I solved this some time ago by class SwitchableConnection extends \yii\db\Connection, but it was quite complicated. I like suggested solution in PR - points out nicely the difference static vs self, though I am curious about tests. Current failure does not seem related to this PR, but to Generators support in JSON helper #19766.

@samdark samdark added this to the 2.0.48 milestone Mar 19, 2023
@rhertogh
Copy link
Contributor

rhertogh commented Mar 29, 2023

@lubosdz
Current failure does not seem related to this PR, but to Generators support in JSON helper

I ran in the same issue. I've created a PR to fix the unit test (#19798).

samdark added a commit to sallfris/yii2 that referenced this issue Mar 30, 2023
samdark added a commit to sallfris/yii2 that referenced this issue May 21, 2023
@samdark samdark modified the milestones: 2.0.48, 2.0.49 May 21, 2023
@bizley bizley modified the milestones: 2.0.49, 2.0.50 Aug 29, 2023
@bizley bizley modified the milestones: 2.0.49.1, 2.0.50 Oct 5, 2023
@samdark
Copy link
Member

samdark commented May 16, 2024

@rhertogh, @bizley is there something to do for this one besides unit test fix that was done?

@bizley
Copy link
Member

bizley commented May 16, 2024

I think we should changed the mentioned call to static (instead of self).

@samdark
Copy link
Member

samdark commented May 16, 2024

OK. @bizley do you have time for a pull request? I plan to release soon.

@rob006
Copy link
Contributor

rob006 commented May 16, 2024

#17346 (comment)

@bizley
Copy link
Member

bizley commented May 16, 2024

Ech, right. Not sure what to do then.

@samdark samdark removed this from the 2.0.50 milestone May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants