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 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了