mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Paginator's CountOutputWalker not remove ORDER BY for MSSQL platform #6671
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @mleko64 on GitHub (Apr 1, 2021).
In
lib/Doctrine/ORM/Tools/Pagination/CountOutputWalker.phpfile on line 89 is condition which prevents ORDER BY clause in sql query for MSSQL platform (prevents error like "The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified."):but this is not enought.
After this condition, the parent method is called (on line 93):
and in
lib/Doctrine/ORM/Query/SqlWalker.phpfile on line 559 is condition which checks if ORDER BY clause should be in sql query or not:and that is OK, but below that, there is another condition (on line 563) where is a main problem:
Although
$AST->orderByClauseis set tonull,$orderBySqlmay be true and the ORDER BY clause will be added to the SQL query. So, this ORDER BY generates error from MSSQL server.The error can be triggered by adding a join to another entity in the query, and the main entity has a field with
@ORM\OrderByannotation.For example if entity "Product" has a field "$parameters" which is a relation (One-To-Many) to "Parameter" entity and this field has a annotation with
@ORM\OrderByand we create a query builder with join to this field "parameters" and we use this query builder to Paginator then ORDER BY clause will be applied and we will get a error from MSSQL database.@Matt-PMCT commented on GitHub (Jul 5, 2025):
This continues to be a problem, which I'm going to summarize again since it has been a while and some things like the initial mssql check have changed.
Lines 58-60 appear to be an attempt to prevent this problem from occurring:
But in line 62 this command calls the SqlWalker.php file, at line 518-527 the orderby clause is skipped due to being null, and then immediately an ORDER BY is added to the query.
Basically the CountOutputWalker attempts to not break the SQL server and then the SQL Walker breaks SQL server.
A short example of this problem is that I have a query that Doctrine generates that I is attempted to be used in a Paginator:
This query is passed to lib/Doctrine/ORM/Tools/Pagination/CountOutputWalker.php which on line 119 wraps the query to be inside of another query. This produces:
SQL Server then throws an error because you included an ORDER BY statement inside a subquery:
I have come up with some ghetto solutions, but don't feel comfortable or capable enough to apply it to such an important project.