Symfony表单插入通过Select2选择的多个值

I have Many to Many relationship between Institute and courses entity. The Bridge Entity is InstituteCourse. because it involves some extra fields too. What I am wanted to achieve to allocate multiple courses to single institute in a single form submit.

class Institutes {
/**
*@ORM\OneToOne(targetEntity="PNC\InstitutesBundle\Entity\InstitutesCourses", mappedBy="institute")
*/
protected $instituteCourses;
}


class Courses {
/**
* @ORM\OneToMany(targetEntity="PNC\InstitutesBundle\Entity\InstitutesCourses", mappedBy="course")
*/
    protected $instituteCourses;
}

class InstitutesCourses {
    /**
     * @ORM\ManyToOne(targetEntity="PNC\InstitutesBundle\Entity\Institutes", inversedBy="instituteCourses")
     * @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;
}

The Form for InstituteCourse.

 $builder->add('institute','entity', array(
                    'class'=>'PNC\InstitutesBundle\Entity\Institutes',
                    'property'=>'name',
                    'label' => 'Institute'
                )
            )
  ->add('course', 'genemu_jqueryselect2_entity', array(
                'class' => 'PNC\CoursesBundle\Entity\Courses',
                'property' => 'courseTitle',
                'multiple' => true,
                'attr' => array(
                    'class' => 'form-control'
                )
            ))

            ->add('save', 'submit', array(
                'label' => 'Save and Return to List',
                'attr' => array(
                    'class' => 'btn btn-success'
                )
            ));

and the controller

  public function newAction(Request $request){
        $em = $this->getDoctrine()->getManager();
        $course = new Courses();
        $institutesCourses = new InstitutesCourses();
        $form = $this->createForm(new InstitutesCoursesType(), $institutesCourses, array(
            'action' => $this->generateUrl('instituteCourses_create'),
            'method' => 'POST',
        ));
        if ($request->getMethod() == 'POST')
        {
            $form->handleRequest($request);
            if ($form->isSubmitted() && $form->isValid()) {
                $data = $form->getData();
                $selectedTag = $form['course']->getData();
                if( $selectedTag != null ){
                    $array = $selectedTag->toArray();
                    $length = count($array);
                    for ($i = 0; $i < $length; $i++) {
                        $course = $em->getRepository('PNCCoursesBundle:Courses')->getCoursesByCourseTitle($array[$i]);
                        $institutesCourses->setCourse($course);
                        $em->persist($institutesCourses);
                    }

                }
                $em->flush();
            }
        }

and the repo function public function getCoursesByCourseTitle($criteria) {

$queryBuilder = $this->_em->createQueryBuilder();
$queryBuilder->select('c')
    ->from('PNCCoursesBundle:Courses', 'c')
    ->Where('c.courseTitle = :q')
    ->setParameter('q', '%'.$criteria.'%');
return $queryBuilder;

}

While Saving it says Prompts that. **

Found entity of type Doctrine\ORM\QueryBuilder on association PNC\InstitutesBundle\Entity\InstitutesCourses#course, but expecting PNC\CoursesBundle\Entity\Courses

**

The Institute and Course entities don't have a direct relationship. You probably want to define a Many to Many relationship with a Join table.

Something like:

class Courses {

/**
 * @ManyToMany(targetEntity="PNC\InstitutesBundle\Entity\Institutes")
 * @JoinTable(name="institutes_courses",
 *      joinColumns={@JoinColumn(name="institute_id", referencedColumnName="id")},
 *      inverseJoinColumns={@JoinColumn(name="course_id", referencedColumnName="id", unique=true)}
 *      )
 */
private $institutes;
}

And the inverse of that on the institute Entity.

See http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-unidirectional-with-join-table