如何使用Doctrine查询非空值?

35

我有一个名为Test的表:

Test:
id | name 
1  | aaa
2  | 
3  | ccc
4  | aaa
5  | 
6  | ddd

我想要结果中name不为空的数据:

aaa
ccc
aaa
ddd

我怎样可以得到:

Doctrine_Core::getTable('Test')->findBy('name', NOTNULL??) <-doesnt working

并且在具有以下模型的情况下:

$this->createQuery('u')
     ->where('name = ?', NOTNULL ???) <- doesnt working
     ->execute();
3个回答

60

尝试这个:

$this->createQuery('u')
     ->where('name IS NOT NULL')
     ->execute();

这是标准的SQL语法。Doctrine不会将Null值转换为正确的SQL语句。


1
你不能这么做,你需要编写自己的定制方法。 - Dziamid

19

采用Doctrine的方式进行操作,使用查询构建器和Expr类。

 $qb = $entityManager->createQueryBuilder();
 $result = $qb->select('t')
        ->from('Test','t')
        ->where($qb->expr()->isNotNull('t.name'))
        ->groupBy('t.name')
        ->getQuery()
        ->getResult();

还有一个distinct()函数。


3

或者只需使用Doctrine过滤器:

$filters[] = new Filter('name', null, 'notEqual');

那么

$list = $this->get(yourDBinstance)
        ->setDocIdentifier('TestBundle:Test')
        ->setFilters($filters)
        ->list();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接