我正在构建一个具有歌曲和评级的应用程序。我需要为当前登录用户选择所有带有其关联的乐曲的歌曲。我尝试这样做,但是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(); } }
尝试addSelect:
addSelect
$qb = $this->getEntityManager()->createQueryBuilder() ->select('s') ->from('RateBundle:Song','s') ->leftJoin('s.ratings','r','WITH','r.user = :user') ->addSelect('r') ->setParameter('user', $user);