-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Update ActiveDataProvider::prepareModels() to avoid SQL error with UNION queries #20246
base: master
Are you sure you want to change the base?
Conversation
cepaim
commented
Aug 16, 2024
Q | A |
---|---|
Is bugfix? | ❌ |
New feature? | ✔️ |
Breaks BC? | ❌ |
Fixed issues | #20239 |
…ION queries As for issue yiisoft#20239, move order by and limit clauses to the last UNION query to avoid SQL errors on UNION queries
Update ActiveDataProvider::prepareModels() to avoid SQL error with UNION queries
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #20246 +/- ##
============================================
- Coverage 64.94% 64.93% -0.01%
- Complexity 11390 11394 +4
============================================
Files 430 430
Lines 36912 36919 +7
============================================
+ Hits 23972 23975 +3
- Misses 12940 12944 +4 ☔ View full report in Codecov by Sentry. |
I don't think this is a correct solution. IMO it should be fixed in yii2/framework/db/QueryBuilder.php Lines 225 to 269 in 34d2396
|
@rob006 You are right! I don't know Yii2 so deeply, but if we fix this at the query level, it will be fixed for all its uses. At the moment, my hack is working for me and I am now very busy. I'll try to find time to make another PR. I think this PR can be rejected and closed. |
@cepaim Does it really work for MySQL? Because only SQLite doesn't have parentheses, for all other DBMS generated query will look like |
@rob006 I'll tell you when I test in on mysql. |
@@ -101,17 +101,29 @@ protected function prepareModels() | |||
throw new InvalidConfigException('The "query" property must be an instance of a class that implements the QueryInterface e.g. yii\db\Query or its subclasses.'); | |||
} | |||
$query = clone $this->query; | |||
|
|||
$has_union = $query->union && count($query->union); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$has_union = $query->union && count($query->union); | |
$hasUnion = $query->union && count($query->union); |
if (($pagination = $this->getPagination()) !== false) { | ||
$pagination->totalCount = $this->getTotalCount(); | ||
if ($pagination->totalCount === 0) { | ||
return []; | ||
} | ||
$query->limit($pagination->getLimit())->offset($pagination->getOffset()); | ||
if ($has_union) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ($has_union) { | |
if ($hasUnion) { |
} | ||
if (($sort = $this->getSort()) !== false) { | ||
$query->addOrderBy($sort->getOrders()); | ||
} | ||
if ($has_union) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ($has_union) { | |
if ($hasUnion) { |
So can we close this one and wait for the other one? |