我有三个实体:Project
,RequestMatcher
和Response
。以下是它们的相关性:
$project->getRequestMatchers(); // Project has many RequestMatchers
$requestMatcher->getActiveResponse(); // RequestMatcher has one active Response
如何编写DQL以获取Response
的所有有效Project
?
$query = $this->getEntityManager()->createQuery(
"SELECT resp FROM $responseClass resp
JOIN $requestMatcherClass req WITH req.project = :project
WHERE resp.id IN(req.active_response_id)"
);
$query->setParameter('project', $project);
我收到以下异常:
[Doctrine\ORM\Query\QueryException]
[Syntax Error] line 0, col 230: Error: Expected Literal, got 'req'
我也尝试了同样的结果:
$query = $this->getEntityManager()->createQuery(
"SELECT resp FROM $responseClass resp
LEFT JOIN resp.requestMatcher req WITH req.project = :project
WHERE resp IN(req.activeResponse)"
);
这是将给我id的
的sqlselect active_response_id from (
select active_response_id from request_matchers where project_id = 1
) t
答案 0 :(得分:0)
我不相信这是最好的方法,但我用以下方法解决了这个问题:
$query = "SELECT resp FROM $responseClass resp
WHERE resp.id IN (
SELECT IDENTITY(req.activeResponse) FROM $requestMatcherClass req
WHERE req.project = :project
)";
答案 1 :(得分:0)
您也可以尝试使用QueryBuilder对象,例如:
$this->getDoctrine()->getRepository('YourBundle:Project')->createQueryBuilder('p')
->join('p.requestMatchers', 'rm', \Doctrine\ORM\Query\Expr\Join::WITH, 'rm.project = :project')
->join('rm.activeReponse', 'ar')
->addSelect('rm.*')
->addSelect('ar.*')
->setParameter('project', $project)
->getQuery()
->execute();