[英]Symfony2 many-to-many does not update inverse side
我擁有以下實體:學生和學習小組
學習小組一切正常。 我可以選擇屬於學習組的學生列表,然后學說創建關聯。
但是,當我想從另一側做同樣的事情(為學生選擇學習小組)時,什么也沒發生。
這是我的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中切換“反轉”和“擁有”側,則該問題也將切換。
我知道這里提到了這一點: http : //doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/unitofwork-associations.html
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"
));
}
我對此感到非常驚訝,因為這似乎表明您可能正在發生無限循環。
addStudent()調用addStudyGroup()調用addStudent()調用addStudyGroup()調用addStudent()............刪除這兩條“ ADDED THIS LINE”,這需要在您的控制器中進行。
這是控制器中的更新。 首先,在創建表格之前,您需要記住該學生現有的學習小組清單:
/* 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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.