mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
Incorrect column alias when using multiple "addSelect" #5489
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 @sboesch on GitHub (Mar 30, 2017).
I'm trying to build the following SQL query with Doctrine QueryBuilder:
The querys purpose is to find all "projects", ordered by "similarity" to a given project. Similarity is calculated by counting matching relationships between projects and foreign tables (eg. project_attribute).
The example above works as expected when executed via MySQL.
Now I tried to build this query via Doctrine:
Unfortunately, "attributeSimilarity" from the first "addSelect" gets aliased to "sclr_31", while it doesn't use the alias in the second "addSelect":
AS sclr_31, (attributeSimilarity * 1) AS sclr_32Column not found: 1054 Unknown column 'attributeSimilarity' in 'field list'Expected result is:
AS sclr_31, (sclr_31 * 1) AS sclr_32Now, I'm not sure - is this a bug or did I something wrong?
Ah, I forgot to mention that the final query contains multiple subqueries which results will be summed up and multiplied to get a final "similarity" value. That's the reason I separated "similarity" into it's own "addSelect".
@Ocramius commented on GitHub (Mar 30, 2017):
This seems wrong - we don't really support sub-
SELECTclauses in theSELECTclause. What's the DQL here?@sboesch commented on GitHub (Mar 30, 2017):
$qb->getDQL() returns this:
Basically I only need this sub selects to order the result by relevance. Any other way to achieve this besides raw SQL?
@Ocramius commented on GitHub (Mar 30, 2017):
My mistake - it seems like the ebnf declares sub-
SELECTstatements. The bug is valid, we just need a minimal reproduction scenario.@sboesch commented on GitHub (Apr 10, 2017):
I'm sorry, I just found out that using Aliases within another Alias isn't even allowed in MySQL.
When I wrote and tested the query above in raw MySQL, I haven't used Aliases but directly added the subqueries COUNT() results together; which works.
Unfortunately this style of writing doesn't work either with DQL.
So I guess this issue isn't actually a bug.
Anyway, is possible to get the desired results with DQL?
I really don't want to use raw MySQL.
Basically I want to write the following:
@Ocramius commented on GitHub (Apr 10, 2017):
Uhm... I would just say to keep it in raw SQL unless there is a strong need
for it to be DQL...
On 10 Apr 2017 4:13 p.m., "sboesch" notifications@github.com wrote:
@sboesch commented on GitHub (Apr 10, 2017):
Ok, thank you, i'll stick to raw SQL...
So the statement above is not supposed to be supported by doctrine?
@Ocramius commented on GitHub (Apr 10, 2017):
@sboesch unless there's a strong reason to do that, it's not worth doing so. The fact that the generated SQL is broken is still a bug though.
@wlalele commented on GitHub (Jul 7, 2017):
@Ocramius if there is an actual strong need for it to be in DQL, how would you do it ?
@stof commented on GitHub (Mar 7, 2018):
What is not supported AFAIK is to refer to a ResultAliasVariable as part of another expression in the select (and so you cannot reference the
attributeSimilarityvariable for the next select expression)@tugrul commented on GitHub (Sep 15, 2020):
This bug also occurs when use
@OrderByannotation.