Doctrine Many to Many查询获取其他边数据

时间:2016-05-11 02:35:23

标签: php symfony doctrine-orm doctrine query-builder

我在institute和“课程”之间有许多关系。

class Institutes {
    /**
     * @ORM\ManyToMany(targetEntity="PNC\CoursesBundle\Entity\Courses", inversedBy="institutes")
     * @ORM\JoinTable(name="institute_courses",
     *      joinColumns={@ORM\JoinColumn(name="institute_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="course_id", referencedColumnName="id")}
     *      )
     */
    protected $courses;
}


class Courses {
    /**
     * @ORM\ManyToMany(targetEntity="PNC\InstitutesBundle\Entity\Institutes", mappedBy="courses")
     */
    protected $institutes;
}

我做了一个表格,在onece指定一个学院的多个课程,它的工作原理。现在我想通过学说查询将这些课程列表分配给学院,我有以下查询,但是ID不起作用。

$em = $this->getDoctrine()->getManager();
        $repository = $em->getRepository('PNCInstitutesBundle:Institutes');
        $query = $repository->createQueryBuilder('i')
            ->innerJoin('i.courses', 'g')
            ->where('i.id = :inst_id')
            ->setParameter('inst_id',3)
            ->getQuery()->getResult();

1 个答案:

答案 0 :(得分:0)

 $repository = $em->getRepository('PNCCoursesBundle:Courses');
                $courses = $repository->createQueryBuilder('i')
                    ->innerJoin('i.institutes', 'g')
                    ->where('g.id = :inst_id')
                    ->setParameter('inst_id',$instituteID)
                    ->getQuery()->getResult();