-
-
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
ActiveDataProvider::prepareModels: wrong queries when using union subqueries and pagination #20239
Comments
Working on this issue, I find impossible to use a union query for a gridview with an activecord, because when I click on the header of a cell to sort its contents, the orderby is added to the main query and I get the error: SQLSTATE[HY000]: General error: 1 ORDER BY clause should come after UNION not before Using Yii2.0.52-dev and php8.1 and mysql 8.0.39 |
The final function that fixes all these problems would be:
|
Do you have time for a pull request? |
Sure, I'll do it ASAP. |
From which branch should I make the pr? |
…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
master, just add test files. |
Have a look at this pr: #20246 |
What steps will reproduce the problem?
Create a query with a union subquery and use it with a paginated dataprovider:
What is the expected result?
No errors should be reported.
What do you get instead?
SQLSTATE[HY000]: General error: 1 LIMIT clause should come after UNION not before
Failed to prepare SQL: SELECT "my_table".* FROM "my_table" WHERE ("my_table".
id
=:qp0) LIMIT 10 UNION SELECT "my_table".* FROM "my_table" WHERE ("my_table".user_id
=:qp2)The problem lies in ActiveDataProvider::prepareModels():
The $query->limit should take into account that if there is a union clause, that limit should be put in the last union query:
There ara also lots of problems with orderBy clauses and unions, but all of them can be sorted out removing all the orderBy clauses and adding just one to the last union subquery, but it would be nice if Yii2 made it for us.
The text was updated successfully, but these errors were encountered: