DQL Syntax Errors when upgrading to 2.6.0 #5829

Closed
opened 2026-01-22 15:19:04 +01:00 by admin · 6 comments
Owner

Originally created by @ohartl on GitHub (Jan 3, 2018).

Originally assigned to: @Ocramius on GitHub.

This DQL and others with the same problem (I anonymized it here) used to work with 2.5.x:

$result = $this->getEntityManager()
    ->createQuery(
        "SELECT a
        FROM SomeProject\Entity\A a
        JOIN SomeProject\Entity\B b
        WHERE b.rel_a_id = a.id AND b.deleted = 0
        JOIN SomeProject\Entity\C c
        WHERE c.id = b.rel_c_id AND c.deleted = 0
        WHERE a.deleted = 0 AND c.some_name = :myNameParam"
    )
    ->setParameters($parameters)
    ->getResult();

Now with 2.6.0 I get this error.

Doctrine\ORM\Query\QueryException: [Syntax Error] line 0, col 273: Error: Expected end of string, got 
'JOIN'
...

Caused by
Doctrine\ORM\Query\QueryException:
        SELECT a
        FROM SomeProject\Entity\A a
        JOIN SomeProject\Entity\B b
        WHERE b.rel_a_id = a.id AND b.deleted = 0
        JOIN SomeProject\Entity\C c
        WHERE c.id = b.rel_c_id AND c.deleted = 0
        WHERE a.deleted = 0 AND c.some_name = :myNameParam

.../vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php:43
.../vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php:452
.../vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php:862
.../vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php:259
.../vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php:355
.../vendor/doctrine/orm/lib/Doctrine/ORM/Query.php:283
.../vendor/doctrine/orm/lib/Doctrine/ORM/Query.php:295
.../vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php:957
.../vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php:912
.../vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php:716
.../SomeProject/Repository/SomeRepository.php:64

Not sure if its my fault not using the correct syntax, this worked in the previous version of doctrine/orm, or its a BC break maybe?!

Originally created by @ohartl on GitHub (Jan 3, 2018). Originally assigned to: @Ocramius on GitHub. This DQL and others with the same problem (I anonymized it here) used to work with 2.5.x: ``` $result = $this->getEntityManager() ->createQuery( "SELECT a FROM SomeProject\Entity\A a JOIN SomeProject\Entity\B b WHERE b.rel_a_id = a.id AND b.deleted = 0 JOIN SomeProject\Entity\C c WHERE c.id = b.rel_c_id AND c.deleted = 0 WHERE a.deleted = 0 AND c.some_name = :myNameParam" ) ->setParameters($parameters) ->getResult(); ```` Now with 2.6.0 I get this error. ``` Doctrine\ORM\Query\QueryException: [Syntax Error] line 0, col 273: Error: Expected end of string, got 'JOIN' ... Caused by Doctrine\ORM\Query\QueryException: SELECT a FROM SomeProject\Entity\A a JOIN SomeProject\Entity\B b WHERE b.rel_a_id = a.id AND b.deleted = 0 JOIN SomeProject\Entity\C c WHERE c.id = b.rel_c_id AND c.deleted = 0 WHERE a.deleted = 0 AND c.some_name = :myNameParam .../vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php:43 .../vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php:452 .../vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php:862 .../vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php:259 .../vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php:355 .../vendor/doctrine/orm/lib/Doctrine/ORM/Query.php:283 .../vendor/doctrine/orm/lib/Doctrine/ORM/Query.php:295 .../vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php:957 .../vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php:912 .../vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php:716 .../SomeProject/Repository/SomeRepository.php:64 ```` Not sure if its my fault not using the correct syntax, this worked in the previous version of doctrine/orm, or its a BC break maybe?!
admin added the BugBC BreakInvalid labels 2026-01-22 15:19:04 +01:00
admin closed this issue 2026-01-22 15:19:04 +01:00
Author
Owner

@Ocramius commented on GitHub (Jan 3, 2018):

Uhh... I didn't even know multiple WHERE worked - that is most likely a bug in your code, and we simply made the DQL align to the [EBNF definition](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#ebnf

@Ocramius commented on GitHub (Jan 3, 2018): Uhh... I didn't even know multiple `WHERE` worked - that is most likely a bug in your code, and we simply made the DQL align to the [EBNF definition](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#ebnf
Author
Owner

@Ocramius commented on GitHub (Jan 3, 2018):

Also, arbitrary join conditions require a WITH clause...

@Ocramius commented on GitHub (Jan 3, 2018): Also, arbitrary join conditions require a `WITH` clause...
Author
Owner

@ohartl commented on GitHub (Jan 3, 2018):

Well, multiple WHERE used to work in 2.5.x, and my coworker had
Replacing the WHERE for joins with WITH fixed the problem. Thanks :)

Btw. shouldn't the documentation use WITH instead of WHERE in the (at least first two) examples?

@ohartl commented on GitHub (Jan 3, 2018): Well, multiple `WHERE` used to work in 2.5.x, and my coworker had Replacing the `WHERE` for joins with `WITH` fixed the problem. Thanks :) Btw. shouldn't the [documentation](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#joins) use `WITH` instead of `WHERE` in the (at least first two) examples?
Author
Owner

@Ocramius commented on GitHub (Jan 3, 2018):

@ohartl they weren't really supposed to work :S

As for the documentation you linked, those are not arbitrary joins, but joins over associations.

@Ocramius commented on GitHub (Jan 3, 2018): @ohartl they weren't really supposed to work :S As for the documentation you linked, those are not arbitrary joins, but joins over associations.
Author
Owner

@ohartl commented on GitHub (Jan 3, 2018):

Ok, thanks

@ohartl commented on GitHub (Jan 3, 2018): Ok, thanks
Author
Owner

@amici commented on GitHub (May 29, 2019):

I'm somewhat late to v2.6.*, but I also stumbled on a similar issue. Here is my case, if there are others with similar misfortune.
In 2.5.14, it was possibly to join two unrelated entities without the WITH keyword, like:

SELECT tableA FROM AppBundle:TableA tableA 
  JOIN AppBundle:TableB tableB 
  WHERE tableA.something = :something ...

Most likely, this would be done by error, and the full cross JOIN would be a result, but still the query would work. With enough results in the WHERE part - maybe even as it should.

Note that this query could also be done if the 2 entities are actually properly related (like many-to-one), and possibly the dev presumed that the JOIN will just automagically do its job.

In v2.5, the query generated from the above would be something like:

SELECT ...
FROM Table_A a0_
  INNER JOIN Table_B a1_
    ON (*ALL-THE-CONDITIONS-FROM-THE-WHERE-PART*)

So, the ORM would put all the WHERE conditions INTO the ON part and make the SQL work.

In v2.6.3 that query simply fails. As far as I saw, the ORM tries to build something like this:

SELECT ...
FROM Table_A a0_
  INNER JOIN Table_B a1_
    WHERE (*ALL-THE-CONDITIONS-FROM-THE-WHERE-PART*)

So, the ORM does not try anymore to be smart and strictly does what it was told: it puts the WHERE conditions to WHERE in the SQL and also completely skips adding the ON part in the join.

But, it eventually complains, with a "Incorrect syntax near the keyword 'WHERE'" error, because it actually expects the JOIN to have the ON part.

Anyway... I had to clear this out for me (and maybe for someone else), because it was an unexpected BC break, even if it was really a case of stop-tolerating-wrong-code BC break.

@amici commented on GitHub (May 29, 2019): I'm somewhat late to v2.6.*, but I also stumbled on a similar issue. Here is my case, if there are others with similar misfortune. In 2.5.14, it was possibly to join two unrelated entities **without** the WITH keyword, like: ``` SELECT tableA FROM AppBundle:TableA tableA JOIN AppBundle:TableB tableB WHERE tableA.something = :something ... ``` Most likely, this would be done by error, and the full cross JOIN would be a result, but still the query would work. With enough results in the WHERE part - maybe even as it should. Note that this query could also be done if the 2 entities are actually properly related (like many-to-one), and possibly the dev presumed that the JOIN will just automagically do its job. In v2.5, the query generated from the above would be something like: ``` SELECT ... FROM Table_A a0_ INNER JOIN Table_B a1_ ON (*ALL-THE-CONDITIONS-FROM-THE-WHERE-PART*) ``` So, the ORM would put all the **WHERE** conditions INTO the **ON** part and make the SQL work. In v2.6.3 that query simply fails. As far as I saw, the ORM tries to build something like this: ``` SELECT ... FROM Table_A a0_ INNER JOIN Table_B a1_ WHERE (*ALL-THE-CONDITIONS-FROM-THE-WHERE-PART*) ``` So, the ORM does not try anymore to be smart and strictly does what it was told: it puts the WHERE conditions to **WHERE** in the SQL and also completely skips adding the **ON** part in the join. But, it eventually complains, with a "*Incorrect syntax near the keyword 'WHERE'*" error, because it actually expects the JOIN to **have** the ON part. Anyway... I had to clear this out for me (and maybe for someone else), because it was an unexpected BC break, even if it was really a case of *stop-tolerating-wrong-code* BC break.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5829