Doctrine QueryBuilder如何做到这一点

时间:2018-06-05 12:55:39

标签: doctrine query-builder

这是symfony。 如何更好地完成这项工作在其他环境上的工作并不知道为什么 河是Recruitment Entity,与RecruitmentUsers有着很好的关系

$this->createQueryBuilder('r')
        ->select('r')
        ->addSelect('(SELECT SUM(b.payedAmount) FROM APP\Entity\RecruitmentUsers b WHERE b.isActive = 1 and r.id = b.recruitment) as payedSum')
        ->addSelect('(SELECT SUM(c.declaredAmount) FROM APP\Entity\RecruitmentUsers c WHERE c.isActive = 1 and r.id = c.recruitment) as declaredSum')
        ->leftJoin('r.recruitmentUsers','u')
        ->groupBy('r.id')
        ->orderBy('r.id', 'DESC')
        ->getQuery()
        ->getResult()

在开发环境中,一切正常,我得到了这个错误:

Fatal error: Uncaught Doctrine\ORM\Query\QueryException: SELECT r, (SELECT SUM(b.payedAmount) FROM APP\Entity\RecruitmentUsers b WHERE b.isActive = 1 and r.id = b.recruitment) as payedSum, (SELECT SUM(c.declaredAmount) FROM APP\Entity\RecruitmentUsers c WHERE c.isActive = 1 and r.id = c.recruitment) as declaredSum FROM App\Entity\Recruitment r LEFT JOIN r.recruitmentUsers u GROUP BY r.id ORDER BY r.id DESC in /usr/home/eliteinvestments/domains/4eliteinvestments.pl/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php:43 Stack trace: #0 /usr/home/eliteinvestments/domains/4eliteinvestments.pl/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(487): Doctrine\ORM\Query\QueryException::dqlError('SELECT r, (SELE...') #1 /usr/home/eliteinvestments/domains/4eliteinvestments.pl/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(987): Doctrine\ORM\Query\Parser->semanticalError('line 0, col 42 ...', Array) #2 /usr/home/eliteinvestments/domains/4eliteinvestments.pl/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(1 in /usr/home/eliteinvestments/domains/4eliteinvestments.pl/templates/bundles/TwigBundle/Exception/error.html.twig on line 6 

我做了cache:clear,cache:warmup,doctrine:cache:clear-query,doctrine:cache:clear-result,doctrine:cache:clear-metadata 它都不起作用。 任何提示?

1 个答案:

答案 0 :(得分:0)

我会尝试这样的事情。不确定您想要什么。

$this->createQueryBuilder('r')
->select('r.id, SUM(u.payedAmount), SUM(u.declaredAmount)')
->leftJoin('r.recruitmentUsers','u')
->where('u.isActive = 1')
->groupBy('r.id')
->orderBy('r.id', 'DESC')
->getQuery()
->getResult();