php - 使用 Doctrine 和 Symfony2 createQueryBuilder leftJoin

标签 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 张 table : 用户:id(主要)、姓名、...
投票:id(主要)、问题、...
答案:id(主要)、idVote、答案...
结果:id(主),idUser(id表用户),idVote(id表投票),idAnswer(id表答案),...

-----编辑----
感谢您的回答,但这不是我所做的。
结果也是一张表。这仅包含 id,我需要返回,例如:
代替:1、6、5、3
此: 1、u.name、v.question、a.answer

最佳答案

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

关于php - 使用 Doctrine 和 Symfony2 createQueryBuilder leftJoin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22108725/

相关文章:

php - Google Maps API 标记未显示 - 使用 MySQL 和 JSON

php - 将多个同名字段作为一条记录插入数据库

mysql - 如何在 SUM 中使用带有 CASE 的 WHERE 子句?

mysql - 按日期和条件关联两个表

php - 如何在 WooCommerce 数据库中找到客户订购的产品?

php - Mysql 重复结果

mysql - 如何对不熟悉的行进行分组?

mysql - "Unrecognized statement type. (near "与 "at position 0)"mysql

sql - sql server 中的 'order by' 错误

javascript - jquery 中的 PHP 代码无法正常工作