如何避免多次使用同一个名字?

How to avoid multiple uses of the same name?

我正在尝试使用 symfony 框架制作这个小项目,它非常简单,但我仍然是新手。 我有 3 个实体,Classe、Student 和 WorkDays。 class 与 Student 有一个 OneToMany 关系,另一个与 schedule 有 OneToMany 关系。 创建 class 时,您可以添加学生和工作日 这是我的代码: 类实体:

<?php

namespace 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;
    }

    /**
     * @return Collection|WorkDays[]
     */
    public function getSchedule(): Collection
    {
        return $this->schedule;
    }

    public function addSchedule(WorkDays $schedule): self
    {
        if (!$this->schedule->contains($schedule)) {
            $this->schedule[] = $schedule;
            $schedule->setClass($this);
        }

        return $this;
    }

    public function removeSchedule(WorkDays $schedule): self
    {
        if ($this->schedule->contains($schedule)) {
            $this->schedule->removeElement($schedule);
            // set the owning side to null (unless already changed)
            if ($schedule->getClass() === $this) {
                $schedule->setClass(null);
            }
        }

        return $this;
    }
}

学生实体:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\StudentRepository")
 */
class Student
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=200)
     */
    private $firstname;

    /**
     * @ORM\Column(type="string", length=200)
     */
    private $lastname;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\classe", inversedBy="Students")
     * @ORM\JoinColumn(nullable=false)
     */
    private $classe;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getFirstname(): ?string
    {
        return $this->firstname;
    }

    public function setFirstname(string $firstname): self
    {
        $this->firstname = $firstname;

        return $this;
    }

    public function getLastname(): ?string
    {
        return $this->lastname;
    }

    public function setLastname(string $lastname): self
    {
        $this->lastname = $lastname;

        return $this;
    }

    public function getClasse(): ?classe
    {
        return $this->classe;
    }

    public function setClasse(?classe $classe): self
    {
        $this->classe = $classe;

        return $this;
    }
}

和工作日

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\WorkDaysRepository")
 */
class WorkDays
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=200)
     */
    private $day;

    /**
     * @ORM\Column(type="time", nullable=true)
     */
    private $start;

    /**
     * @ORM\Column(type="time")
     */
    private $end;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Classe", inversedBy="schedule")
     */
    private $class;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getDay(): ?string
    {
        return $this->day;
    }

    public function setDay(string $day): self
    {
        $this->day = $day;

        return $this;
    }

    public function getStart(): ?\DateTimeInterface
    {
        return $this->start;
    }

    public function setStart(?\DateTimeInterface $start): self
    {
        $this->start = $start;

        return $this;
    }

    public function getEnd(): ?\DateTimeInterface
    {
        return $this->end;
    }

    public function setEnd(\DateTimeInterface $end): self
    {
        $this->end = $end;

        return $this;
    }

    public function getClass(): ?Classe
    {
        return $this->class;
    }

    public function setClass(?Classe $class): self
    {
        $this->class = $class;

        return $this;
    }
}

最后这是我的表格,我使用 CollectionType 来包括学生和工作日,我使用这个插件:https://github.com/ninsuo/symfony-collection 添加我想要的学生和工作日

<?php

class ClasseType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('Label')



            ->add('Students', CollectionType::class, [
                'label'        => 'Students',
                'entry_type'   => StudentsType::class,
                'allow_add'    => true,
                'allow_delete' => true,
                'prototype'    => true,
                'required'     => false,
                'by_reference' => false,
                'delete_empty' => true,
                'attr'         => [
                    'class' => 'collection',
                ],
            ])

           ->add('schedule', CollectionType::class, [
            'label'        => 'schedule',
            'entry_type'   => scheduleType::class,
            'allow_add'    => true,
            'allow_delete' => true,
            'prototype'    => true,
            'required'     => false,
            'by_reference' => false,
            'delete_empty' => true,
            'attr'         => [
                'class' => 'collection',
            ],
        ])


        ;
                $builder->add('save', SubmitType::class, [
                'label' => 'See my addresses',
        ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Classe::class,
        ]);
    }
}

现在,一切正常,以这种形式添加学生也会将他们添加到他们的 respectable table 中。工作日也是如此,让您从实体类型下拉菜单中选择日期

我的问题: 有什么办法可以避免重复的学生姓名和工作日姓名?与 class 名称相同 ?

设置属性的唯一属性,就像评论中提到的那样必须是唯一的。你可以捕获 Doctrine 的任何 QueryException 并将适当的错误 message/response 发送回客户端。