我有实体:学生和学习小组
研究组的一切正常。我可以选择属于学习小组的学生列表,并且Doctrine创建关联。
然而,当我想从另一方做同样的事情(为学生选择学习小组)时,没有任何反应。
以下是我的YAML文件配置的重要部分
StudyGroups.orm.yml
manyToMany:
students:
targetEntity: Students
inversedBy: studyGroups
cascade: ["persist"]
joinTable:
name: students_study_groups
joinColumns:
study_groups:
referencedColumnName: id
inverseJoinColumns:
students:
referencedColumnName: id
Students.orm.yml
manyToMany:
studyGroups:
targetEntity: StudyGroups
mappedBy: students
cascade: ["persist"]
lifecycleCallbacks: { }
我还在StudyGroups.php实体文件中尝试了以下修改
public function addStudent(\AppBundle\Entity\Students $students) {
$students->addStudyGroup($this); // ADDED THIS LINE
$this->students[] = $students;
return $this;
}
在Students.php实体文件中相同
public function addStudyGroup(\AppBundle\Entity\StudyGroups $studyGroups) {
$studyGroups->addStudent($this); // ADDED THIS LINE
$this->studyGroups[] = $studyGroups;
return $this;
}
我发现这个问题的有趣之处在于,如果我在StudyGroups.orm.yml和Students.orm.yml中切换Inverse和Owning方面,问题也会切换。
11.2。重要概念
忽略仅对关联的反面进行的更改。确保更新双向关联的两端(或者至少是从Doctrine的角度来看的拥有方)
这是我的问题。我的反面变化被忽略了。我怎样才能解决这个问题 ?我尝试了其他用户提到的所有内容,但仍然无效。提前谢谢。
更新1
以下是我编辑学生的方法。请注意,如果我切换反向和拥有方(在YAML文件配置中),研究组编辑将起作用,并且学生编辑将不起作用。 (通过不工作,我的意思是它不会创建两者之间的关联)
public function editStudentAction(Request $request, $student) {
/* DATABASE SERVICE */
$this->db_helper = $this->get('db.helper');
/* DOCTRINE */
$em = $this->getDoctrine()->getManager();
$student = $em->getRepository('AppBundle:Students')->find($student);
/* INVALID ID */
if (count($student) == 0) {
/* INVALID ID */
$request->getSession()->getFlashBag()->add(
'error', 'Invalid ID!'
);
return $this->redirect($this->generateUrl('index'));
}
/* GET STUDENT FORM */
$form = $this->newStudentForm($student, "edit");
/* HANDLE FORM SUBMISSION */
$form->handleRequest($request);
if ($form->isValid()) {
/* CHECK IF STUDENT ONLY WANT TO ENROLL 2 STUDY GROUPS */
if (count($student->getStudyGroups()) > 2) {
$request->getSession()->getFlashBag()->add(
'error', 'Students may only enroll 2 study groups at the same time!'
);
} else {
try {
/* SAVE TO DATABASE */
$em->persist($student);
$em->flush();
/* REDIRECT TO MAIN PAGE */
return $this->redirect($this->generateUrl('index'));
} catch (\Doctrine\DBAL\DBALException $e) {
/* HANDLE DUPLICATE KEY ISSUE */
if ($e->getPrevious()->getCode() === '23000') {
$request->getSession()->getFlashBag()->add(
'error', 'User already exists!'
);
} else
throw $e;
}
}
}
return $this->render('default/new.html.twig', array(
'form' => $form->createView(),
'object' => "student"
));
}
答案 0 :(得分:0)
我非常惊讶这两方都有效,因为看起来你可能会发生潜在的无限循环。
addStudent()调用addStudyGroup()调用addStudent(),调用addStudyGroup()调用addStudent()............删除这两个"添加此行" ,这需要在你的控制器中发生。
这是您的控制器中的更新。首先,您需要在创建表单之前记住该学生的现有学习小组列表:
/* GET ORIGNAL COLLECTIONS */
$originalGroups = new ArrayCollection(); // requires: use Doctrine\Common\Collections\ArrayCollection;
foreach ($student->getStudyGroups() as $group) {
$originalGroups->add($group);
}
/* GET STUDENT FORM */
现在您需要从新集合中删除任何已删除的研究组
/* MANY TO MANY */
foreach ($originalGroups as $group) {
if (!$student->getStudyGroups()->contains($group)) {
$student->removeStudyGroup($group);
$group->removeStudent($student);
$em->remove($group);
} else {
$group->addStudent($student);
$em->persist($group);
}
}
/* SAVE TO DATABASE */
$em->persist($student);