[英]select objects by joining relationships with doctrine DQL
I have three entities: Project
, RequestMatcher
, and Response
. 我有三个实体: Project
, RequestMatcher
和Response
。 Here is how they are related: 它们之间的关系如下:
$project->getRequestMatchers(); // Project has many RequestMatchers
$requestMatcher->getActiveResponse(); // RequestMatcher has one active Response
How can i write DQL to get all active Response
s for a Project
? 我怎么能写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);
I'm getting the following exception though: 我收到以下异常:
[Doctrine\ORM\Query\QueryException]
[Syntax Error] line 0, col 230: Error: Expected Literal, got 'req'
I've also tried this with the same result: 我也尝试了相同的结果:
$query = $this->getEntityManager()->createQuery(
"SELECT resp FROM $responseClass resp
LEFT JOIN resp.requestMatcher req WITH req.project = :project
WHERE resp IN(req.activeResponse)"
);
This is the sql that will give me the id's 这是将给我id的sql
select active_response_id from (
select active_response_id from request_matchers where project_id = 1
) t
I'm not convinced this is the best way to do it but i solved this with the following: 我不认为这是最好的方法,但是我通过以下方法解决了这个问题:
$query = "SELECT resp FROM $responseClass resp
WHERE resp.id IN (
SELECT IDENTITY(req.activeResponse) FROM $requestMatcherClass req
WHERE req.project = :project
)";
You can also try using the QueryBuilder object, something like : 您也可以尝试使用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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.