doujiao7679 2015-01-16 12:24
浏览 70
已采纳

Symfony2&Dooctrine Where子句使用连接

I have a query like this;

$builder = $this->createQueryBuilder("p")
        ->where("p.published = :published AND p.mission.game_system = :game_system")
        ->orderBy("p.date_published", "DESC")
        ->setMaxResults($limit)
        ->setParameter("published", true)
        ->setParameter("game_system", $game_system);

p is a project.

I want to only get projects for a specific game system, but the game system is stored in the mission. I cannot add the game system to the project as this creates a recursive reference.

How do I enable the where to get projects for a specific game system only?

For example this would get all projects where the mission id is 1;

$builder = $this->createQueryBuilder("p")
        ->where("p.published = :published AND p.mission = :mission")
        ->orderBy("p.date_published", "DESC")
        ->setMaxResults($limit)
        ->setParameter("published", true)
        ->setParameter("mission", 1);

But I want all the projects that relate to the game system, which is stored inside of the mission row. The mission and game system have a manyToOne relationship.

For example I want all projects where the game system id is 5 regardless of the mission, something like this, but this fails

$builder = $this->createQueryBuilder("p")
        ->where("p.published = :published AND p.mission.game_system = :game_system")
        ->orderBy("p.date_published", "DESC")
        ->setMaxResults($limit)
        ->setParameter("published", true)
        ->setParameter("game_system", 5);
  • 写回答

1条回答 默认 最新

  • doufu3718 2015-01-16 12:31
    关注

    It seems that this works, but is it the right approach?

    $builder = $this->createQueryBuilder("p")
        ->innerJoin('p.mission', 'm')
        ->innerJoin('m.game_system', 'gs')
        ->where("p.published = :published AND gs = :game_system")
        ->orderBy("p.date_published", "DESC")
        ->setMaxResults($limit)
        ->setParameter("published", true)
        ->setParameter("game_system", $game_system);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀