因此,我的SQL的目标是获取Apartments及其中的一些信息。
这是SQL:
SELECT apartment.id, AVG(review.staff), COUNT(distinct review.id), city.name as city_name, state.state as state_name,
MIN(room.price_per_night)
FROM room
LEFT JOIN apartment ON room.apartment_id=apartment.id
LEFT JOIN review ON room.apartment_id=review.apartment_id
LEFT JOIN city ON apartment.city_id = city.id
LEFT JOIN state ON city.city_state_id = state.id
GROUP BY apartment.id;
有效。
但是当我尝试将其转换为DQL时,一切顺利,直到我尝试加入评论表:
$qb = $em->createQueryBuilder('c');
$qb->select("MIN(r.price_per_night), rev.id")
->from("TechforgeApartmentBundle:Room", 'r')
->leftJoin('r.apartment', 'a')
->leftJoin('Review', 'rev', \Doctrine\ORM\Query\Expr\Join::WITH, 'r.apartment = rev.apartment')
->groupBy('a.id');
它总是抱怨在以前没有发现评论:
[语义错误]第0行,第116行附近' rev with r.apartment':错误: 在连接路径表达式中使用的标识变量检查但是 之前没有定义。
我尝试了很多,似乎没有任何帮助。
答案 0 :(得分:2)
我很确定你可以放弃WITH条件,假设Apartment和Review有一个映射的关联。
$qb->select('MIN(r.price_per_night), rev.id')
->from('TechforgeApartmentBundle:Room', 'r')
->leftJoin('r.apartment', 'a')
->leftJoin('a.review', 'rev')
->groupBy('a.id');
如果由于某种原因你需要WITH条件,语法如下:
->leftJoin('a.review', 'rev', 'WITH', 'a.id = rev.apartment_id')
答案 1 :(得分:0)
我通过使用原始SQL查询解决了这个问题:
$em = $this->getDoctrine()->getEntityManager();
$stmt = $em->getConnection()
->prepare("...
");
$stmt->execute();