Doctrine createQuery()/ iterate()无法在SQL中处理IF stmt?

时间:2013-11-25 11:00:04

标签: doctrine-orm doctrine

        $qb = $this->em->createQuery("
            SELECT 
                label.value                    AS label,
                'link'                         AS productLink,
                descr.value                    AS description,
                IF (a.is_new = 1, 'new', NULL) AS 'condition',
            ....
        ");
        $results = $qb->iterate();

给出错误:

exception 'Doctrine\ORM\Query\QueryException' with message
[Syntax Error] line 0, col 514: Error:
Expected known function, got IF
in /var/www/xxx/library/Doctrine/ORM/Query/QueryException.php:42

当我将SQL放入phpMyAdmin时,它有效,..这是Doctrine2中的一个错误吗?

堆栈跟踪:

堆栈追踪:

#0 /var/www/xxx/library/Doctrine/ORM/Query/Parser.php(380): Doctrine\ORM\Query\QueryException::syntaxError('line 0, col 514...')
#1 /var/www/xxx/library/Doctrine/ORM/Query/Parser.php(3001): Doctrine\ORM\Query\Parser->syntaxError('known function', Array)
#2 /var/www/xxx/library/Doctrine/ORM/Query/Parser.php(2975): Doctrine\ORM\Query\Parser->CustomFunctionDeclaration()
#3 /var/www/xxx/library/Doctrine/ORM/Query/Parser.php(1907): Doctrine\ORM\Query\Parser->FunctionDeclaration()
#4 /var/www/xxx/library/Doctrine/ORM/Query/Parser.php(1048): Doctrine\ORM\Query\Parser->SelectExpression()
#5 /var/www/xxx/library/Doctrine/ORM/Query/Parser.php(758): Doctrine\ORM\Query\Parser->SelectClause()
#6 /var/www/xxx/library/Doctrine/ORM/Query/Parser.php(727): Doctrine\ORM\Query\Parser->SelectStatement()
#7 /var/www/xxx/library/Doctrine/ORM/Query/Parser.php(213): Doctrine\ORM\Query\Parser->QueryLanguage()
#8 /var/www/xxx/library/Doctrine/ORM/Query/Parser.php(288): Doctrine\ORM\Query\Parser->getAST()
#9 /var/www/xxx/library/Doctrine/ORM/Query.php(230): Doctrine\ORM\Query\Parser->parse()
#10 /var/www/xxx/library/Doctrine/ORM/Query.php(241): Doctrine\ORM\Query->_parse()
#11 /var/www/xxx/library/Doctrine/ORM/AbstractQuery.php(686): Doctrine\ORM\Query->_doExecute()
#12 /var/www/xxx/library/Doctrine/ORM/Query.php(525): Doctrine\ORM\AbstractQuery->iterate(Array, 1)

1 个答案:

答案 0 :(得分:0)

不,这不是错误。 Doctrine2不支持IF语句 - CASE语句可以实现相同的功能:

 $qb = $this->em->createQuery("
     SELECT 
         label.value                    AS label,
         'link'                         AS productLink,
         descr.value                    AS description,
         (CASE 
             WHEN (a.is_new = :val) THEN 'new' 
             ELSE NULL
         END) AS condition,
         ....
 ")
 ->setParameter('val' , 1);
 $results = $qb->iterate();