我尝试制作这样的查询构建器:
<?php
$left = $this->em->getRepository(Recommendation::class)
->createQueryBuilder('r')
->select('count(r) as cnt')
->leftJoin('r.procedure', 'd')
->where('d.label is not NULL')
->andWhere('d.id in di.id')
->groupBy('d.label')
->getDQL()
$dict = $this->em->getRepository(\model\Dictionary\Dictionaryitem::class)
->createQueryBuilder('di')
->select('di.id, di.label')
->addSelect('(' .$left. ') as tt')
->join('di.dictionary', 'dd')
->where('dd.id = \'procedures\'')
->getQuery()
->getResult()
;
我得到的是:
预期的Doctrine \ ORM \ Query \ Lexer :: T_FROM,得到'标签'
“标签”指向以下片段“(SELECT p.label label ,count(r)”
我做错了什么?我记得,我从dql做了这样的子选择,它曾经工作过。
修改
我稍微修改了一下查询构建器,以简化消息。
我希望得到的SQL看起来像这样:
SELECT di.id, di.label,
(SELECT count(r) as cnt FROM recommendation as r
LEFT JOIN dictionary_dictionaryitem as d on r.procedure_id = di.id
where d.label is not null
and d.id = di.id
group by d.label) as tt
from dictionary_dictionaryitem as di join dictionary_dictionary as dd on di.dictionary_id = dd.id where dd.id = 'procedures'
;
编辑2
问题解决了。以下代码确实有效。不确定是什么帮助,但它给出了我想要的结果。
$left = $this->em->getRepository(Recommendation::class)
->createQueryBuilder('r1')
->select('count(r1)')
->leftJoin('r1.procedure', 'p1')
->where('p1.label is not NULL')
->andWhere('r1.status in (:status)')
->andWhere('p1.id = di.id')
->groupBy('p1.label')
->getDQL()
;
$dict = $this->em->getRepository(\model\Dictionary\Dictionaryitem::class)
->createQueryBuilder('di')
->select('di.label')
->addSelect('(' . $left . ') as tt')
->join('di.dictionary', 'd')
->where('d.id = \'procedures\'')
->setParameters([
'status' =>array_keys(RecommendationStatus::getRecommendationHasLeftStatuses())
])
->getQuery()
->getResult()
;