mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Subqueries in arithmetic expressions #6169
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 @iluuu1994 on GitHub (Feb 6, 2019).
Bug Report
Summary
I have a query that looks something like this:
Basically,
activityCountshould be the sum of the number of comments and discussions the user has authored.Current behavior
Currently, this results in a parser error.
https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/dql-doctrine-query-language.html#arithmetic-expressions
Assuming the documentation is accurate,
ArithmeticPrimarywill not parse subselects. Thus this is an invalid query.Is this expected behavior?
@SenseException commented on GitHub (Feb 7, 2019):
As far as I know this is an expected behaviour. This DQL query isn't something where you need an ORM. Cases like this should be handled with basic SQL.
@iluuu1994 commented on GitHub (Feb 7, 2019):
🤔
Everything can be handled with SQL. That's not why we use Doctrine. As mentioned in another thread.
Is there a technical reason why this isn't allowed? If not I'll try to make it work and create a PR.
@Ocramius commented on GitHub (Feb 9, 2019):
Not really, mostly complexity, plus the fact that it wasn't built so far. I'd say this is a good chance to include it in 3.x with the new parser, but the inner query must be guaranteed to be an aggregation query with a single scalar result in order to compile (no
GROUP BY)@Ocramius commented on GitHub (Feb 9, 2019):
Also, I agree with @SenseException that this is perfectly OK if built with SQL: the ORM isn't really needed for most read-intensive operations.
@iluuu1994 commented on GitHub (Feb 9, 2019):
Thanks for the response guys. Despite my fear of being annoying I'd like to touch on that again:
I genuinely don't understand this statement. An ORM isn't ever "needed". We use Doctrine because it's convenient and it helps us write better software. Some reasons are:
The second you write a native queries all of these go out the window. I can speak from experience that it's extremely easy to forget or improperly handle one of these things.
Let me give you a practicle example. This is how we usually structure our repositories:
Instead of writing individual queries by hand we use query builders to compose them. This allows us to keep the code as DRY as possible. The options often include things like searching by name, tags, etc. This is then used all over the place.
One request was to allow sorting by activities. I planned to add a new option (the original code from this issue).
Writing this as a native query I will have to:
I hope you can understand that this is simply not a practical solution for us.
@kconde2 commented on GitHub (Nov 10, 2022):
Any update with this ?
I got same issue when I tried to write this in DQL
Did you find a workaround ?
@worthwhileindustries commented on GitHub (Jan 28, 2024):
Painful. Bumping... anyone have a workaround for this? Doing it this way was already a workaround for another reason DQL couldn't do something. Now, I find myself looking for a workaround for the workaround...
@GonZOO82 commented on GitHub (Jan 28, 2024):
@worthwhileindustries this my old but similar code, try out this logic
@worthwhileindustries commented on GitHub (Jan 29, 2024):
@GonZOO82 Thanks for the reply but, I'm not sure if can pickup what you are putting down. This is my DQL and I wrote the lexer code and it spits out DQL fine when I
getDQLbut, the fails on thegetSQL**EDIT: I was missing a closing parens on my lexer which fixed the problem. Thanks for the rubber duck
The error from getSQL ... not sure if there is another way to extend the lexer or the sqlwaker from here or not? Any thoughts?
[Syntax Error] line 0, col 468: Error: Expected Doctrine\ORM\Query\Lexer::T_FROM, got ')'