为了账号安全,请及时绑定邮箱和手机立即绑定

如何避免多次使用同名?

如何避免多次使用同名?

PHP
慕标琳琳 2021-05-05 13:16:53
我正在尝试使用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并将适当的错误消息/响应发送回客户端。


查看完整回答
反对 回复 2021-05-14
  • 1 回答
  • 0 关注
  • 146 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号