如何在 DQL 中按 NULL 排序? [英] How can I order by NULL in DQL?
问题描述
我正在使用 Symfony2 框架和 Doctrine ORM 构建一个应用程序.我有一张航空公司的表格,其中缺少一些 IATA 代码.我正在输出一个列表,按此 IATA 代码排序,但我得到了一个不良结果,即 IATA 代码为空的记录排在最前面.
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 this IATA code, but I'm getting the undesirable result that the records with null IATA codes are sorted at the top.
在 MySQL 中,这很简单,使用 ORDER BY ISNULL(code_iata), code_iata
,但我不知道 DQL 的等价物是什么.我试过
In MySQL this is simple enough to do, with ORDER BY ISNULL(code_iata), code_iata
but I'm clueless as to what the equivalent would be for DQL. I tried
$er->createQueryBuilder('airline')->orderBy('ISNULL(airline.codeIata), airline.codeIata', 'ASC')
但这给了我一个语法错误.
but this gives me a syntax error.
Doctrine 文档也没有给我任何答案.有办法吗?
The Doctrine docs give me no answer either. Is there a way?
推荐答案
DQL 并不包含普通 SQL 的所有功能.幸运的是,您可以定义您的自定义 DQL 方法来完成此操作.
DQL does not contain every function of plain SQL. Fortunately you can define your custom DQL method to accomplish this.
一些资源:
http://punkave.com/window/2012/07/24/for-the-php-crowd-adding-custom-functions-to-doctrine-2-dql
http://docs.doctrine-project.org/en/2.1/cookbook/dql-user-defined-functions.html
http://symfony.com/doc/2.0/cookbook/doctrine/custom_dql_functions.html
这篇关于如何在 DQL 中按 NULL 排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!