我与研究所和课程实体之间有多对多的关系。 Bridge Entity是InstituteCourse。因为它也涉及一些额外的领域。 我希望通过单一表格提交给单个学院分配多门课程。
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;
}
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'
)
));
和控制器
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();
}
}
和repo功能 公共职能getCoursesByCourseTitle($ criteria){
$queryBuilder = $this->_em->createQueryBuilder();
$queryBuilder->select('c')
->from('PNCCoursesBundle:Courses', 'c')
->Where('c.courseTitle = :q')
->setParameter('q', '%'.$criteria.'%');
return $queryBuilder;
}
虽然保存它说提示。 **
在关联时找到Doctrine \ ORM \ QueryBuilder类型的实体 PNC \ InstitutesBundle \ Entity \ InstitutesCourses#course,但期待 PNC \ CoursesBundle \实体\课程
**
答案 0 :(得分:0)
学院与课程实体没有直接关系。 您可能希望使用Join表定义Many to Many关系。
类似的东西:
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;
}
与研究所实体的相反。