Proper way to update bidirectional Many to Many relationship symfony2-doctrine

前端 未结 4 1001
庸人自扰
庸人自扰 2020-12-10 20:35

I did some research, and after reading this and this (and all the related questions) I still can\'t figure which is the proper way to update a many to many relationship in S

4条回答
  •  醉酒成梦
    2020-12-10 21:00

    According to doctrine documentation it will only check the owning side of an association for changes.

    http://doctrine-orm.readthedocs.org/en/latest/reference/unitofwork-associations.html

    So the best way is keep owning side entity assotiations updated.

    In this case you must remove and add support_log in student main in add and remove methods

    class support_log
    {
    /**
    * @ORM\ManyToMany(targetEntity="student_main", mappedBy="support_log")
    **/
    private $student;
    
    public function addStudent($student) {
      $this->student[] = $student;
      $student->addSupportLog($this);
    }
    
    public function removeStudent($student) {
      $student->removeSupportLog($this);
      $this->student->removeElement($student);
    }
    

    Thats all no need to modify controller actions. Important implement this at the inverse side of association!

提交回复
热议问题