关联如何使用学说映射多对一或两个?

时间:2015-11-03 20:24:12

标签: php symfony doctrine-orm

我有两个实体,StudentResponsible(父亲/母亲或导师)。我必须将这两个信息保存在数据库中,所以我必须将两者联系起来。我认为最好的方法是在Many-To-One表中添加每个负责人(最多两个)的外键Student。我不知道这种关联是否可行,或者是否有必要建立一个Many-To-Many关联。

 use Doctrine\ORM\Mapping as ORM;

 class Student{
  ....
 /** 
  *@ORM\ManyToOne(targerEntity="myBundle\Entity\Responsible") 
  *@ORM\JoinColumn(name="responsible_id", referencedColumnName="id")
  */
  protected $responsible;
 }

我对这个主题有点新意,我感谢任何可能的帮助。

3 个答案:

答案 0 :(得分:2)

更改为多对多

我会改为Many-To-Many这意味着一个Responsible可以负责几个学生(Student的集合),学生可以有很多责任人({{1}的集合}}):

Responsible

<?php use Doctrine\ORM\Mapping as ORM; class Responsible { /** *@ORM\ManyToMany(targetEntity="myBundle\Entity\Student", mappedBy="responsibles") */ private $students; /** * It is important to initialize your $students collection */ public function __construct(){ $students = new ArrayCollection } // ALL STUDENT SETTERS + GETTERS /** * Get students * * @return Collection */ public function getStudents() { return $this->students; } /** * Add student. * * @param Student $student * @return self */ public function addStudent(Student $student) { $this->students[] = $student; return $this; } /** * Add students. * * @param Collection $students * @return self */ public function addStudents(Collection $students) { foreach ($students as $student) { $this->addStudent($student); } return $this; } /** * Remove student. * * @param Student $student */ public function removeStudent(Student $student) { $this->students->removeElement($student); } /** * Remove students. * * @param Collection $students * @return self */ public function removeStudents(Collection $students) { foreach ($students as $student) { $this->removeStudent($student); } return $this; } }

Student

替代解决方案

如果您确实要限制一名学生的责任金额,那么您也可以将这两位负责人直接添加到学生<?php use Doctrine\ORM\Mapping as ORM; class Student { /** *@ORM\ManyToMany(targetEntity="myBundle\Entity\Responsible", mappedBy="student") * @ORM\JoinTable(name="student_responisble", * joinColumns={@ORM\JoinColumn(name="student_id", referencedColumnName="id")}, * inverseJoinColumns={@ORM\JoinColumn(name="responsible_id", referencedColumnName="id")} * ) */ private $responsibles; /** * It is important to initialize your $responsibles collection */ public function __construct(){ $responsibles = new ArrayCollection } // ALL RESPONSIBLE SETTERS + GETTERS /** * Get responsibles * * @return Collection */ public function getResponsibles() { return $this->responsibles; } /** * Add responsible. * * @param Responsible $responsible * @return self */ public function addResponsible(Responsible $responsible) { $this->responsibles[] = $responsible; return $this; } /** * Add responsibles. * * @param Collection $responsibles * @return self */ public function addResponsibles(Collection $responsibles) { foreach ($responsibles as $responsible) { $this->addResponsible($responsible); } return $this; } /** * Remove responsible. * * @param Responsible $responsible */ public function removeResponsible(Responsible $responsible) { $this->responsibles->removeElement($responsible); } /** * Remove responsibles. * * @param Collection $responsibles * @return self */ public function removeResponsibles(Collection $responsibles) { foreach ($responsibles as $responsible) { $this->removeResponsible($responsible); } return $this; } } $firstResponsible并添加自定义$secondResponsible方法所以你可以在一个数组中一次性获取它们。如果有必要,您稍后也可以添加getResponsibles

$thirdResponsible

答案 1 :(得分:1)

那么,一个学生可以有一个以上的负责人(导师,父亲或母亲),并且可能导师可以指导一个以上的学生,或者父母可能有一个以上的孩子是学生。 / p>

因此,多对多关系在这里效果最好。即使您有两个负责实体的限制,根据您提供的信息,仍有许多要求。

答案 2 :(得分:1)

当然这是可能的,因为它并不复杂。

如果一个ManyToMany可以拥有多个学生,那么您可以使用Responsible关联,只要它足以在您的ManyToOne实体中使用双向Student关联:

use Doctrine\ORM\Mapping as ORM;

class Student
{
    /** 
     *@ORM\ManyToOne(targetEntity="myBundle\Entity\Responsible", inversedBy="student")
     */
    private $responsible;
}

并且您的Responsible实体应为:

use Doctrine\ORM\Mapping as ORM;

class Responsible
{
    /** 
     *@ORM\OneToMany(targetEntity="myBundle\Entity\Responsible", mappedBy="responsible")
     */
    private $student;
}
如果您想对字段使用标准命名,则不需要

JoinColumnfield_id

如果您不打算从实体继承,也应该使用private属性。

请阅读写得很好的官方Symfony documentationDoctrine documentation,了解有关该主题的更多信息。