Symfony / Doctrine QueryBuilder加入WITH不过滤掉行

时间:2014-02-28 08:55:05

标签: php sql symfony doctrine-orm

我正在建立一个我有歌曲和评级的应用程序。我需要为当前登录的用户选择所有具有相关评级的歌曲。我尝试这样做,但WITH子句不起作用。它不断获取每首歌曲的所有评分。

class SongRepository extends EntityRepository
{
    public function getAllSongsWithRatings($section, $user)
    {
        $qb = $this->getEntityManager()->createQueryBuilder()
           ->select('s')
           ->from('RateBundle:Song','s')
           ->leftJoin('s.ratings','r','WITH','r.user = :user')
           ->setParameter('user', $user);
        return $qb->getQuery()->getResult();
    }
}

1 个答案:

答案 0 :(得分:3)

尝试addSelect

$qb = $this->getEntityManager()->createQueryBuilder()
       ->select('s')
       ->from('RateBundle:Song','s')
       ->leftJoin('s.ratings','r','WITH','r.user = :user')
       ->addSelect('r')
       ->setParameter('user', $user);