Reputation: 6723
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();
}
}
Upvotes: 0
Views: 1063
Reputation: 2560
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);
Upvotes: 4