简体   繁体   中英

Symfony/Doctrine QueryBuilder Join WITH not filtering out rows

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();
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM