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_outc
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:
if (count($terms) > 1 || $pos !== false) {
if($terms[0]=='case')
{
$terms=explode(" as ", $reference);
$expression = array_shift($terms);
$alias = array_pop($terms);
if ( ! $alias) {
$alias = substr($expression, 0, $pos);
}
$componentAlias = $this->getExpressionOwner($expression);
$tableAlias = $this->getTableAlias($componentAlias);
$expression=str_replace($componentAlias, $tableAlias, $expression);
$index=0;
$sqlAlias = $tableAlias . '__' . $alias;
}
else
{
$expression = array_shift($terms);
$alias = array_pop($terms);
if ( ! $alias) {
$alias = substr($expression, 0, $pos);
}
$componentAlias = $this->getExpressionOwner($expression);
$expression = $this->parseClause($expression);
$tableAlias = $this->getTableAlias($componentAlias);
$index = count($this->_aggregateAliasMap);
$sqlAlias = $this->_conn->quoteIdentifier($tableAlias . '__' . $index);
}
$this->_sqlParts['select'][] = $expression . ' AS ' . $sqlAlias;
It's not a great change, but it helped me...
Case statements do appear to have been added to doctrine at some point: https://github.com/doctrine/orm-documentation/commit/189c729f15d2fafecf92662cad9553c2ec3dccd7#diff-0
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:
$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();
The BNF grammar for the Doctrine Query Language doesn't seem to contain anything related to a CASE
construct.
As mentioned by adavea, Doctrine now has added support for CASE expressions. You can do something like
$resultset = Doctrine_Query::create()
->select("t.code, t.description")
->addSelect("CASE WHEN(t.id_outcome = 1) THEN 1 ELSE 0 END as in_progress")
->from('LuOutcome t')
->orderBy('t.rank')
->fetchArray();
Hope this might help someone, thanks!