mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
'CASE WHEN...' ok with addSelect() and addOrderBy(), but KO with addGroupBy()
#6994
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 @spacecodeur on GitHub (Jun 17, 2022).
Bug Report
Summary
On Symfony 6 and Doctrine 2.12.3, I build a new query.
In this query, I need to use 'CASE WHEN ...' in
SELECT,ORDER BYandGROUP BY.With the methods
addSelect()andaddOrderBy()all works fine. But I got an error if I useCASE WHENin the parameter of the functionaddGroupBy(), the error is :'Cannot group by undefined identification or result variable'
The bug still the same on sqlite3 or mariadb
Current behavior
For
SELECTandORDER BY, all work fine :The query works without any error.
But if I use
CASE WHEN ...in aaddGroupBy()function :I get the error : ''Cannot group by undefined identification or result variable''
How to reproduce
With query builder, just add a
addGroupByusingCASE WHENand you'll get the error 'Cannot group by undefined identification or result variable'' if you execute the query.I created a demo project for illustrate the bug. The link of the repo is here.
Here the commands for clone and deploy a local server for easily see the bug :
Open the URL https://127.0.0.1:9123 with your favorite browser. And then, you'll see the home page where the issue is showed :
And if you click on the link, to the bottom, you'll be redirected to a page using the query using
addGroupBy()withCASE WHEN ...and get the error :Expected behavior
The query does not return an error. Use
GROUP BY CASE WHEN ...return the expected result.With DB Browser tool, I get the generated query in the bundle profiler of Symfony. And I added
GROUP BY CASE WHEN .... The query is executed without any error and the expected result of the query is showed :Here the query if you want to copy/past :
temporary solution
After the query builder, I can extract the SQL raw query (with
$query->getQuery()->getSQL()). And then, add in the string theGROUP BY CAS WHEN ....But with this approach, I can't get results as entity objects and its very annoying...
If someone has a better solution for skirt this (apparently) bug, I take !
I have an huge building query on my website (containing several conditions, and take more than 200 lines). I really really need to use
groupBy()withCASE WHENhaha@greg0ire commented on GitHub (Jun 17, 2022):
I don't think it's a bug, see https://www.doctrine-project.org/projects/doctrine-orm/en/2.12/reference/dql-doctrine-query-language.html#items
GroupByItem ::= IdentificationVariable | ResultVariable | SingleValuedPathExpressionI think
ResultVariablemight help you:Try aliasing the case to a variable, then group by that variable?