mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Doctrine QueryBuilder changes JOIN order - Is this expected behavior? #7469
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 @berkut1 on GitHub (Jan 30, 2025).
ORM: 2.20
I’m not sure how to phrase my question and even is this a bug or not, so I’ll just describe the situation.
I have a complex query where I initially used subqueries in JOINs. While refactoring, I decided to split the subquery into multiple JOINs. The final query looks like this:
Since I initially wrote the query as RAW SQL, I expected Doctrine to strictly follow the order in which the JOINs are written. However, that turned out not to be the case.
Doctrine actually executes the query in a different order (simplified example):
As a result, I got an error stating that Doctrine couldn’t resolve alias
bbecause it rearranged theJOINorder.After hours of trial and error, I finally found a solution:
In Doctrine, an alias is not just an alias - it also determines the execution order of JOINs.
So, I had to change my aliases like this:
This effectively creates a dependency chain between aliases and JOINs: fromAlias → Alias.
My question is:
Is this the expected behavior? I couldn't find anything in the documentation (or maybe I missed it) stating that aliases determine the JOIN execution order, overriding the written order of the query.
Thanks!