如何使用学说从 symfony 中的多对多关系中删除单个记录?

How to remove a single record from many to many relationship in symfony usiing doctrine?

这里是两个class具有相关功能的 部分 class 与学生 class

有多对多关系
class Section
{       
    /**
     * @ORM\ManyTOMany(targetEntity="Student",inversedBy="sections")
     */ 
    private $students;

    public function __construct() {
        $this->students = new ArrayCollection();
    }

    /**
     * Add students
     *
     * @param \Blogger\sectionBundle\Entity\Student $students
     * @return Section
     */
    public function addStudent(\Blogger\sectionBundle\Entity\Student $students)
    {
        $this->students[] = $students;

        return $this;
    }

    /**
     * Remove students
     *
     * @param \Blogger\sectionBundle\Entity\Student $students
     */
    public function removeStudent(\Blogger\sectionBundle\Entity\Student $students)
    {
        $this->students->removeElement($students);
    }

    /**
     * Get students
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getStudents()
    {
        return $this->students;
    }
}

   class Student {

    /**
     * @ORM\ManyToMany(targetEntity="Section", mappedBy="students")
     */
    private $sections;

    /**
     * @ORM\Column(type="string")
     */
    protected $studentId;



    public function __construct() {
        $this->sections = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add sections
     *
     * @param \Blogger\sectionBundle\Entity\Section $sections
     * @return Student
     */
    public function addSection(\Blogger\sectionBundle\Entity\Section $sections)
    {
        $this->sections[] = $sections;

        return $this;
    }

    /**
     * Remove sections
     *
     * @param \Blogger\sectionBundle\Entity\Section $sections
     */
    public function removeSection(\Blogger\sectionBundle\Entity\Section $sections)
    {
        $this->sections->removeElement($sections);
    }

    /**
     * Get sections
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getSections()
    {
        return $this->sections;
    }
}

如 mysql

DELETE from student_section
where student_id = (select student.id from student where student.name="dummy")
And section_id = 1

有什么问题:

public function removeStudent(Student $student)
{
    $this->students->removeElement($student);
}

您可以使用通用学说命令生成 getter 和 setter。

app/console doctrine:generate:entities NameSpace:Entity

您还应该阅读有关使用 Doctrine 同步 ManyToMany 双向关系的信息。 Here 您可以阅读有关 "adders" 的内容,但同样的逻辑也适用于删除方法。

编辑 - 问题更新后

如果我对你的理解是正确的,当你知道学生姓名时,你想从一个部分中删除一个学生。创建的 student_section 是从 Doctrine 生成的 table。您可以在您的控制器或存储库中执行正常的 PDO 语句,但我个人会在模型中实现一个函数以使其尽可能保持 OOP。

public function removeStudentByName(Student $student)
{
    $toRemove = $this->students->filter(function (Student $s) use ($student) {
        return ($->getName() == $student->getname());
    });

    foreach ($toRemove as $student) {
        $this->students->remove($student);
    }

}

在控制器中,您可以执行以下操作:

//$student, $em is fetched
$section->removeStudentByName($student);
$em->flush();

抱歉我的误导和不清楚的问题 我找到了我要找的东西

//in the controller:
    $section = $em->getRepository('BloggersectionBundle:Section')->find(2);
    $student = $em->getRepository('BloggersectionBundle:Student')->findByStudentId("555555");
    $student->removeSections($section);
    $em->flush();

在学生模型中

public function removeSections(Section $sections)
    {   
        $sections->removeStudent($this);
        $this->sections->removeElement($sections);
    }

最后我编辑了学生和部分的注释 级联移除

 * @ORM\ManyToMany(targetEntity="Section", mappedBy="students", cascade={"persist", "remove"})