How can I order by NULL in DQL?

后端 未结 6 766
野趣味
野趣味 2021-01-02 20:18

I\'m building an app using Symfony2 framework and using Doctrine ORM. I have a table with airlines for which some IATA codes are missing. I\'m outputting a list, ordered by

6条回答
  •  不知归路
    2021-01-02 20:38

    If you want to do something similar to "NULLS LAST" in SQL (with PostgreSQL in my case):

    ORDER BY freq DESC NULLS LAST
    

    You can use the COALESCE function with the Doctrine Query Builder (HIDDEN will hide the field "freq" on your query result set).

    $qb = $this->createQueryBuilder('d')
               ->addSelect('COALESCE(d.freq, 0) AS HIDDEN freq')
               ->orderBy('freq', 'DESC')
               ->setMaxResults(20);
    

提交回复
热议问题