createQueryBuilder leftJoin使用Doctrine和Symfony2

时间:2014-03-01 01:04:46

标签: php mysql sql symfony doctrine-orm

此代码不起作用。有谁能够帮我?代码是:

class ResultRepository extends EntityRepository
{
     public function findAllByUserResultVote($id_vote)
     {
        $query = $this->createQueryBuilder('r')
           //associating tables...
           ->leftJoin('r.user' , 'u')
           ->leftJoin('r.answer' , 'a')
           ->leftJoin('r.vote' , 'v')

           //Where idVote (Table result) == id (Table vote)
           ->where('r.idVote = :idVote')
           ->setParameter('idVote', $id_vote)
           ->getQuery();
        return $query->getResult();
     }
}

我有一个实体用户,答案,投票,结果。

我有4张桌子: 用户:id(主要),名称,...
投票:id(主要),问题,...
答案:id(主要),idVote,回答...
结果:id(主要),idUser(id表用户),idVote(id表投票),idAnswer(id表回答),...

- - - - 修改 ----
谢谢你的答案,但那不是我的所作所为 结果也是一张表。这只包含id,我需要返回例如:
而不是: 1,6,5,3 这个: 1,u.name,v.question,a.answer

1 个答案:

答案 0 :(得分:0)

public function findAllByUserResultVote($idVote)
 {
    $queryBuilder = $this->createQueryBuilder('r')

    // From your question, it looks like you only want ids
       ->select('r.id, u.name, v.question, a.answer')

    // join with Vote Entity
       ->leftJoin('r.vote' , 'v')
    // join with User Entity
       ->leftJoin('r.user' , 'u')
    // join with Answer Entity
       ->leftJoin('r.answer' , 'a')

    // where id is same as $idVote
       ->andWhere('v.id = :id_vote')
       ->setParameter('id_vote', $idVote);

    return $queryBuilder
        ->getQuery()
        ->getResult();
 }