How to ORDER BY CASE in Doctrine2 (Symfony2)

前端 未结 4 636
鱼传尺愫
鱼传尺愫 2020-12-28 14:45

I want to run this query by using Doctrine in Symfony 2.3. But it seems like Doctrine does not understand CASE statement. Can anyone help? Thank you in advance!



        
4条回答
  •  离开以前
    2020-12-28 15:23

    If you are using createQueryBuilder then you can use like

    $query->addSelect("(CASE WHEN name like 'John %' THEN 0
               WHEN name like 'John%' THEN 1
               WHEN name like '% John%' THEN 2
               ELSE 3 END) AS HIDDEN ORD ");
    $query->orderBy('ORD', 'DESC');
    

    Note that you must have "HIDDEN".

    You can do with doctrine native query as well.

提交回复
热议问题