'CASE WHEN...' ok with addSelect()
and addOrderBy()
, but KO with addGroupBy()
#9857
spacecodeur
started this conversation in
General
Replies: 1 comment 1 reply
-
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
I think
Try aliasing the case to a variable, then group by that variable? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
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 BY
andGROUP BY
.With the methods
addSelect()
andaddOrderBy()
all works fine. But I got an error if I useCASE WHEN
in the parameter of the functionaddGroupBy()
, the error is :'Cannot group by undefined identification or result variable'
I get the same error on sqlite3 or mariadb
Current behavior
For
SELECT
andORDER 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
addGroupBy
usingCASE WHEN
and 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 WHEN
hahaBeta Was this translation helpful? Give feedback.
All reactions