我正在尝试使用symfony框架来制作这个小项目,这很简单,但是我仍然很陌生。我有3个实体,即Classe,Student和WorkDays。班级与学生有一个OneToMany关系,而与日程表有另一个OneToMany关系。创建课程时,您可以在这里添加学生和WorkDay,这是我的代码:Classe实体:<?phpnamespace App\Entity;use Doctrine\Common\Collections\ArrayCollection;use Doctrine\Common\Collections\Collection;use Doctrine\ORM\Mapping as ORM;/** * @ORM\Entity(repositoryClass="App\Repository\ClasseRepository") */class Classe{ /** * @ORM\Id() * @ORM\GeneratedValue() * @ORM\Column(type="integer") */ private $id; /** * @ORM\Column(type="string", length=200) */ private $label; /** * @ORM\OneToMany(targetEntity="App\Entity\Student", mappedBy="classe", orphanRemoval=true) */ private $Students; /** * @ORM\OneToMany(targetEntity="App\Entity\WorkDays", mappedBy="class") */ private $schedule; public function __construct() { $this->Students = new ArrayCollection(); $this->schedule = new ArrayCollection(); } public function getId(): ?int { return $this->id; } public function getLabel(): ?string { return $this->label; } public function setLabel(string $label): self { $this->label = $label; return $this; } /** * @return Collection|Student[] */ public function getStudents(): Collection { return $this->Students; } public function addStudent(Student $student): self { if (!$this->Students->contains($student)) { $this->Students[] = $student; $student->setClasse($this); } return $this; } public function removeStudent(Student $student): self { if ($this->Students->contains($student)) { $this->Students->removeElement($student); // set the owning side to null (unless already changed) if ($student->getClasse() === $this) { $student->setClasse(null); } } return $this; }我的问题: 有什么办法可以避免重复学生姓名和工作日姓名?与班级名称相同吗?
1 回答

qq_花开花谢_0
TA贡献1835条经验 获得超7个赞
设置属性的唯一属性,这些属性必须是唯一的,如注释中所述。而且,您可以捕获Doctrine的任何QueryException并将适当的错误消息/响应发送回客户端。
- 1 回答
- 0 关注
- 146 浏览
添加回答
举报
0/150
提交
取消