I have a select query I'd like to perform with Doctrine:
$resultset = Doctrine_Query::create()
->select("t.code, t.description, case when t.id_outcome = 1 then 1 else 0 end as in_progress")
->from('LuOutcome t')
->orderBy('t.rank')
->fetchArray();
And it barfs on the 'case'. The documentation does not mention that it's possible (or not).
I'm wondering if Doctrine lacks the capacity to do so. If so, it's a rather major omission. Does anyone know of a work-around?
The BNF grammar for the Doctrine Query Language doesn't seem to contain anything related to a
CASE
construct.I recommend you to not use this CASE syntax for solving this problem. It looks tricky.
Why don't you want to
and then loop through $resultset and create this field (in_progress) manually depending on (id_outcome) value. You can use some small simple tiny method for that.
Benefits:
I just had the same problem and, at first glance, appear to have a workaround. I believe you can 'fool' the Doctrine Select parser into treating it as a sub-query by wrapping it in parenthesis.
Give this a try:
Case statements do appear to have been added to doctrine at some point: https://github.com/doctrine/orm-documentation/commit/189c729f15d2fafecf92662cad9553c2ec3dccd7#diff-0
As mentioned by adavea, Doctrine now has added support for CASE expressions. You can do something like
Hope this might help someone, thanks!
I had the same problem here. My project is very old, and tried to fix it quickly. So I just change a little bit the code for Doctrine so I can use "case when". This is my code for Doctrine 1.1.3.
Doctrine/Query.php, change lines from 658 to 674:
It's not a great change, but it helped me...