mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
Arithmetic Operations in OrderBy with Aliases #7428
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 @danielbattat on GitHub (Oct 9, 2024).
Bug Report
Summary
I am trying to sort by the sum of columns, which are in turn subqueries with aliases.
For example, charge_amount is the sum of the customer's total charges, and payment amount is the sum of their payments. I want to sort by their balance (charges less payments):
When I do this, I get the error
Doctrine\\ORM\\Query\\QueryException Expected end of string, got '-'This comment suggests wrapping the orderBy with ()
But then I get
Expected Doctrine\\ORM\\Query\\Lexer::T_IDENTIFIER, got '('If I wrap with double (( )):
I then get error
Notice: Undefined index: charge_amount- it seems to lose track of the alias.I also tried:
and get
Argument 1 passed to Doctrine\\ORM\\Query\\AST\\ParenthesisExpression::__construct() must be an instance of Doctrine\\ORM\\Query\\AST\\Node, string givenI am using Doctrine/orm 2.7 and symfony 4.4.
Any help will be very appreciated - have been struggling with this for 2 days. Thanks for your help!
Current behavior
Errors instead of sorted results.
How to reproduce
See code samples above.
Expected behavior
Query should return results. MySQL and other databases allow for arithmetic with aliased columns in order by.