使用doctrine2中的不同值SELECT * FROM

时间:2015-06-08 10:57:47

标签: php sql doctrine-orm

所以我没有什么问题,我有这样的代码:

$query = $repository->createQueryBuilder('p')->
                    where('p.kalorie <= :kalorie AND p.cena <= :kwota AND p.deser = :sniadanie')->
                    setParameters(array(
                        'kalorie' => $kalorie, 
                        'kwota' => $kwota,
                        'sniadanie' => 1))->getQuery(); 
$wynik = $query->getResult();

但我想做点什么:

if id = 1 then 'kwota' => $kwota + 20
if id = 2 then 'kwota' => $kwota + 55
if id = n then 'kwota' => $kwota + x

我的意思是:

SELECT * FROM Recipe WHERE (id = $id AND kwota < ($kwota + 20)) OR (id = $id2 AND kwota < ($kwota + 55)) OR (id = $idN AND kwota < ($kwota + x))

1 个答案:

答案 0 :(得分:0)

如果您对SQL查询有疑问,可以使用&#39; expr&#39; class:http://doctrine-orm.readthedocs.org/en/latest/reference/query-builder.html#the-expr-class

如果它是关于php变量的,你可以使用三元运算符:

$query = $repository->createQueryBuilder('p')->
                where('p.kalorie <= :kalorie AND p.cena <= :kwota AND p.deser = :sniadanie')->
                setParameters(array(
                    'kalorie' => $kalorie, 
                    'kwota' => $id == 1? $kwota + 20 : ($id == 2 ? $kwota + 55 : $kwota + x),
                    'sniadanie' => 1))->getQuery(); 
$wynik = $query->getResult();