在 DQL 和 symfony 查询中使用 IFNULL

use IFNULL in DQL and symfony query

我项目中的子查询和查询生成器 但是当在查询中使用 ```IFNULL`` 命令时 return eroor 我的代码如下

  $subQb = $em->createQueryBuilder();
            $subquery = $subQb->select('COUNT(v.id)')
                ->from('AdminBundle:Visitsite', 'v')
                ->where('v.site = s.id')
                ->Andwhere('v.createdate > :date')
                ->getDQL();

            $subQb2 = $em->createQueryBuilder();
            $subquery2 = $subQb2->select('l.quantity')
                ->from('AdminBundle:Limitviewday', 'l')
                ->where($subQb2->expr()->eq('s.limitviewday', 'l.id'))
                ->getDQL();


            $subQb3 = $em->createQueryBuilder();
            $subquery3 = $subQb3->select('COUNT(i.id)')
                ->from('AdminBundle:Visitsite', 'i')
                ->where('i.id = s.id')
                ->Andwhere('i.createdate > :date2')
                ->Andwhere('i.ip = :ip')
                ->groupBy('i.ip')
                ->getDQL();

            $subQb4 = $em->createQueryBuilder();
            $subquery4 = $subQb4->select('ipl.quantity')
                ->from('AdminBundle:Iplimitview', 'ipl')
                ->where('s.iplimitview = ipl.id')
                ->getDQL();


            $qb = $em->createQueryBuilder();
            $query = $qb->select('s')
                ->from('AdminBundle:Sites', 's')
                ->where('s.quantity > :one')
                ->Andwhere('s.status = :two')
                ->Andwhere($qb->expr()->lt("($subquery)", "($subquery2)"))
                ->Andwhere($qb->expr()->lt("(SELECT IFNULL(  ($subquery3),0) )", "($subquery4))"))
                ->setParameter('one', 1)
                ->setParameter('two', 1)
                ->setParameter('date', $date->format('Y-m-d'))
                ->setParameter('ip', $ip)
                ->setParameter('date2', $date->format('Y-m-d 00:00:00'));

我的结果是

[Syntax Error] line 0, col 274: Error: Expected known function, got 'IFNULL'

Doctrine 的映射函数集有限,默认情况下似乎没有 IFNULL。

简单的方法是更改​​您的 DQL 并将 IFNULL 替换为 IF 或 COALESCE。

比较难读的方法manual and implement your own IFNULL mapping like this