php 如何使用 Doctrine 查询 NOT NULL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7305468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to query NOT NULL with Doctrine?
提问by Michael Fidy
I have table Test:
我有表测试:
Test:
id | name
1 | aaa
2 |
3 | ccc
4 | aaa
5 |
6 | ddd
I want result where name is NOT NULL:
我想要名称不为空的结果:
aaa
ccc
aaa
ddd
How can i get with:
我怎样才能得到:
Doctrine_Core::getTable('Test')->findBy('name', NOTNULL??) <-doesnt working
and in model with:
并在模型中:
$this->createQuery('u')
->where('name = ?', NOTNULL ???) <- doesnt working
->execute();
回答by Dziamid
Try this:
尝试这个:
$this->createQuery('u')
->where('name IS NOT NULL')
->execute();
which is standard SQL syntax. Doctrine doesn't convert Null values into proper sql.
这是标准的 SQL 语法。Doctrine 不会将 Null 值转换为正确的 sql。
回答by Oshan Wisumperuma
Do it in Doctrine way, from query builder and Expr class.
从查询构建器和 Expr 类以 Doctrine 方式进行。
$qb = $entityManager->createQueryBuilder();
$result = $qb->select('t')
->from('Test','t')
->where($qb->expr()->isNotNull('t.name'))
->groupBy('t.name')
->getQuery()
->getResult();
there are also distinct() function.
还有distinct()函数。