【问题标题】:How to query NOT NULL with Doctrine?如何使用 Doctrine 查询 NOT NULL?
【发布时间】:2011-11-10 10:51:35
【问题描述】:

我有表测试:

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

我想要名称不为 NULL 的结果:

aaa
ccc
aaa
ddd

我怎样才能得到:

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

在模型中:

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

【问题讨论】:

    标签: php symfony1 doctrine symfony-1.4 doctrine-1.2


    【解决方案1】:

    试试这个:

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

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

    【讨论】:

    • 你不能,你需要编写自己的自定义方法。
    【解决方案2】:

    从查询构建器和 Expr 类以 Doctrine 方式进行。

     $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();
      

      【讨论】:

        猜你喜欢
        • 2017-07-08
        • 1970-01-01
        • 2019-06-16
        • 2018-09-12
        • 2022-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-29
        相关资源
        最近更新 更多