dql sql中的错误在symfony中使用COALESCE

时间:2016-10-29 18:04:06

标签: php mysql symfony doctrine

我在查询中使用dql和子查询 编译我的代码时返回错误请帮帮我 我的代码是

$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.site = 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 COALESCE(($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'));
            $settlements = $query->getQuery()->getResult();

和dql字符串结果是

SELECT s FROM AdminBundle:Sites s WHERE s.quantity > :one AND s.status = :two AND ((SELECT COUNT(v.id) FROM AdminBundle:Visitsite v WHERE v.site = s.id AND v.createdate > :date) < (SELECT l.quantity FROM AdminBundle:Limitviewday l WHERE s.limitviewday = l.id)) AND ((SELECT COALESCE((SELECT COUNT(i.id) FROM AdminBundle:Visitsite i WHERE i.site = s.id AND i.createdate > :date2 AND i.ip = :ip GROUP BY i.ip),0)) < (SELECT ipl.quantity FROM AdminBundle:Iplimitview ipl WHERE s.iplimitview = ipl.id))

但我的结果是

  

[语法错误]第0行,第284行:错误:预期文字,得到'SELECT'

请帮帮我

0 个答案:

没有答案