简体   繁体   English

Symfony主义一对多查询生成器

[英]Symfony Doctrine One to many querybuilder

I have one to many releationship between institute and institute courses. 我在学院和学院的课程之间有一对多的关系。

class Institutes {
  /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @ORM\OneToMany(targetEntity="PNC\InstitutesBundle\Entity\InstitutesCourses", mappedBy="institute", cascade={"all"})
     * */
    protected $inst;
}

class InstitutesCourses {
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @ORM\ManyToOne(targetEntity="PNC\InstitutesBundle\Entity\Institutes", inversedBy="inst")
     * @ORM\JoinColumn(name="institute_id", referencedColumnName="id")
     */
    protected $institute;
    /**
     * @ORM\ManyToOne(targetEntity="PNC\CoursesBundle\Entity\Courses", inversedBy="instituteCourses")
     * @ORM\JoinColumn(name="course_id", referencedColumnName="id")
     */
    protected $course;
}

I want to fetch all courses assign to one courses. 我想获取分配给一门课程的所有课程。 each institute is owned by a user. 每个机构均归用户所有。 I have wrote this psql query that is working well 我写了这个运行良好的psql查询

SELECT ic.*
FROM institutes_courses ic
RIGHT JOIN institutes i ON i.id = ic.institute_id
WHERE i.user_id = 26;

 id  | institute_id | course_id |
-----+--------------+-----------+
 389 |           21 |        51 |
 390 |           21 |        53 |
 391 |           21 |        52 |

and translated into doctinre querybuilder like this; 并像这样翻译成doctinre querybuilder;

$repository = $em->getRepository('PNCInstitutesBundle:InstitutesCourses');

            $query= $repository->createQueryBuilder('ic')
                ->select('ic')
                ->from('PNCInstitutesBundle:InstitutesCourses', 'ic')
                ->orderBy('ic.id', 'ASC')
                 // THE FOLLOWING LINE IS CRITICAL:
                ->JOIN('PNCInstitutesBundle:Institutes', 'i', 'WITH' ,'i.id=ic.institute')
                ->where('i.user = :user')
                ->setParameter('user', $this->getUser()->getId())
                ->getQuery();

and it says that, 它说,

[Semantical Error] line 0, col 170 near 'ic INNER JOIN': Error: 'ic' is already defined. [语义错误]第0行,'ic INNER JOIN'附近的col 170:错误:'ic'已定义。

new to symfony2 doctrine, cant figure out what's wrong. Symfony2准则的新手,无法找出问题所在。

if you have a look at the source code of createQueryBuilder you'll see the following: 如果您查看createQueryBuilder的源代码,则会看到以下内容:

public function createQueryBuilder($alias, $indexBy = null)
{
    return $this->_em->createQueryBuilder()
        ->select($alias)
        ->from($this->_entityName, $alias, $indexBy);
}

Which means you should not do the following in your code. 这意味着您不应在代码中执行以下操作。

->select('ic')
->from('PNCInstitutesBundle:InstitutesCourses', 'ic')

Just drop that lines and it should be ok. 只需删除这些行,就可以了。 Repository already did it for you. 存储库已经为您做到了。

Try this: 尝试这个:

(EDIT) (编辑)

$repository = $em->getRepository('PNCInstitutesBundle:InstitutesCourses');

$query= $repository->createQueryBuilder('ic')
     ->leftJoin('ic.institute', 'i')
     ->where('i.user = :user')
     ->setParameter('user', $this->getUser()->getId())
     ->orderBy('ic.id', 'ASC')
     ->getQuery();

$results = $query->getResult();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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