【问题标题】:Symfony2 & Dooctrine Where clause using a joinSymfony2 和 Dooctrine Where 子句使用连接
【发布时间】:2015-01-16 12:24:16
【问题描述】:

我有一个这样的查询;

$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 是一个项目。

我只想获取特定游戏系统的项目,但游戏系统存储在任务中。我无法将游戏系统添加到项目中,因为这会创建递归引用。

如何让where 仅获取特定游戏系统的项目?

例如,这将获取任务 id 为 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);

但我想要所有与游戏系统相关的项目,这些项目存储在任务行内。任务和游戏系统是多对一的关系。

例如,我想要所有游戏系统 id 为 5 的项目,而不管任务如何,类似这样,但失败了

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

【问题讨论】:

    标签: php symfony join doctrine-orm where


    【解决方案1】:

    这似乎可行,但这是正确的方法吗?

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-02
      • 1970-01-01
      相关资源
      最近更新 更多