如何在symfony usiing doctrine中从多对多关系中删除单个记录?

时间:2015-11-08 17:16:43

标签: php symfony model-view-controller doctrine

这是涉及功能的两个类 section 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

3 个答案:

答案 0 :(得分:0)

错误:

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

答案 1 :(得分:0)

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

app/console doctrine:generate:entities NameSpace:Entity

另外,您应该阅读有关使用Doctrine同步ManyToMany双向关系的信息。 Here您可以阅读"加法器" ,但同样的逻辑适用于删除方法。

编辑 - 问题更新后

如果我理解正确,您希望在拥有学生姓名时从某个部分中删除学生。创建的student_section是来自Doctrine的生成表。您可以在控制器或存储库中执行普通的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();

答案 2 :(得分:0)

抱歉我的误导性和不明确的问题 我找到了我在寻找的东西

//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);
    }

最后我在学生和部分编辑了anotation 级联删除

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