如何用原则查询NOT NULL


How to query NOT NULL with Doctrine?

我有一个表Test:

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

我想要的结果是name不是NULL:

aaa
ccc
aaa
ddd

我怎样才能得到:

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

和in model with:

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

试试这个:

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

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

以原则的方式,从查询生成器和Expr类。

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

还有distinct()函数

或者直接使用Doctrine filter:

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

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