Doctrine 2 (used in Symfony v6) : impossible to use "GROUP BY" with "CASE WHEN ..." #9855
Unanswered
spacecodeur
asked this question in
Support Questions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
On my website, I build a complex DQL query (more than 200 lines). This DQL query works like a charm.
Now, at the end of the query, I want to GROUP BY some results depending some values of some columns. Here a simplified example :
With SQL I can easily handle that with "GROUP BY" and "CASE WHEN...":
You can try it here if you want
On Symfony (version 6) and Doctrine (version 2.9), I already use "CASE WHEN..." with the doctrine method
addSelect(...)
. (and its work)But, now, I want use "CASE WHEN..." in the method
addGroupBy
and its not work ans I don't understand why... (maybe a doctrine limitation ?)From my repository, in DQL query, if I try to use "CASE WHEN" with a groupBy like this :
$query->addGroupBy('CASE WHEN content.periodicity ...')
I get the error :
[Semantical Error] line 0, col 205 near 'CASE WHEN content.periodicity': Error: Cannot group by undefined identification or result variable.
For debugging, I get the SQL query generated by doctrine from profiler bundle, then I add manually the SQL part
GROUP BY CASE WHEN content.periodicity ...
and try it from a database software (I use DB Browser). And all work without any error :For information, the link between 'content' entity and 'periodicity' entity is ManyToOne. I tried to change
content.periodicity
bycontent.periodicity_id
but I get the same error.My questions :
Is a limitation of Doctrine (v2.9) ? In other words, Doctrine can't use
CASE WHEN ...
with GroupBy ?If no, what/where is my mystake ?
if yes, is it possible to insert native SQL part in a DQL query ? (if yes, I'll can add 'manually' in my code the SQL part
GROUP BY CASE WHEN ...
)Thank for any help :)
Beta Was this translation helpful? Give feedback.
All reactions