mirror of
https://github.com/doctrine/orm.git
synced 2026-04-29 17:33:15 +02:00
Unable to sort DQL Query Results by a value computed by a SubQuery #7302
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 @eXsio on GitHub (Jan 23, 2024).
Bug Report
I want to select a custom DTO that contains, as one of its fields, a value computed by a SubQuery that is defined in the Selection List. Furthermore, I need to sort the Result by the value computed by that SubQuery.
Summary
There is currently no way (known to me) to sort a Criteria Query result by a value computed by a SubQuery. None of the things I've tried works:
orderBy()methodorderBy()methodCurrent behavior
All attempts end up with some kind of Doctrine Parser error:
[Syntax Error] line 0, col 178: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got 'as'[Syntax Error] line 0, col 247: Error: Expected Doctrine\ORM\Query\Lexer::T_IDENTIFIER, got '('[Syntax Error] line 0, col 247: Error: Expected Doctrine\ORM\Query\Lexer::T_IDENTIFIER, got '3'[Syntax Error] line 0, col 247: Error: Expected Doctrine\ORM\Query\Lexer::T_IDENTIFIER, got ':columnNumber'How to reproduce
A full reproduction will all methods mentioned in this Ticket is here.
Expected behavior
At least one of the proposed ways should work, or there should be another way to sort the Criteria Query Result by the result of the SubQuery.
@eXsio commented on GitHub (Jan 25, 2024):
Can I get some feedback on this? Is this a bug or rather a missing feature? To me it looks more like a bug (or at least a big inconsistency), because we can add aliases to other things like columns or even complex expressions. I don't see any reason for not being able to do the same for SubQueries. The lack of such possibility severely cripples the entire feature of having SubQueries in the Selection List.
@pich commented on GitHub (Aug 29, 2024):
👀 IMHO it's a bug
@domino91 commented on GitHub (Aug 29, 2024):
yep, that's a problem
@tomasvts commented on GitHub (Oct 22, 2024):
I recently came across this issue, and for anyone looking for a workaround, it can be resolved using a table subquery: