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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?