Symfony/Doctrine QueryBuilder Join WITH not filter

2019-04-17 08:19发布

I am building an application where I have Songs and Ratings. I need to select all Songs with its associated Ratings for the current logged in user. I try to do this, but the WITH clause is not working. It keeps fetching all ratings for each song.

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条回答
Anthone
2楼-- · 2019-04-17 09:02

Try with addSelect :

$qb = $this->getEntityManager()->createQueryBuilder()
       ->select('s')
       ->from('RateBundle:Song','s')
       ->leftJoin('s.ratings','r','WITH','r.user = :user')
       ->addSelect('r')
       ->setParameter('user', $user);
查看更多
登录 后发表回答