du5739 2011-11-12 16:11
浏览 53
已采纳

Doctrine 2 / MySQL:无法将SQL左连接转换为DQL左连接

So, the goal of my SQL is to get Apartments and some information about them.

This is the 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;

which works.

But when I try to turn it to DQL, everything goes fine until I try to join the review table:

    $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');

It always complains about Review not being identified before:

[Semantical Error] line 0, col 116 near 'rev WITH r.apartment': Error: Identification Variable Review used in join path expression but was not defined before.

I tried alot, and nothing seemed to help.

  • 写回答

2条回答 默认 最新

  • dpj96988 2011-12-13 01:43
    关注

    I solved this by using raw sql queries:

            $em = $this->getDoctrine()->getEntityManager();
            $stmt = $em->getConnection()
                       ->prepare("...
                 ");
            $stmt->execute();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?