【问题标题】:Symfony's Unique Entity not working at allSymfony 的唯一实体根本不起作用
【发布时间】:2019-05-25 12:49:53
【问题描述】:

我使用 Symfony 已经有一段时间了,并且我已经在另一个项目中使用 Unique Entity 进行了验证,并且成功了。我正在尝试创建一个新项目,但由于某种原因,Unique Entity 不会阻止我插入重复的字段。

$timetable = new Timetable();

    $form = $this->createFormBuilder()
        ->add("name", TextType::class, ["label"=>"Nazwa","attr"=>["class"=>"form-control"]])
        ->add(
            "year",
            ChoiceType::class,
            ["label" => "Rok", "choices" => $years, "attr" => ["class" => "form-control"]]
        )
        ->add(
            "month",
            ChoiceType::class,
            ["label" => "Miesiąc", "choices" => $months, "attr" => ["class" => "form-control"]]
        )
        ->add("dodaj",
            SubmitType::class,
            ["attr"=>["class"=>"btn btn-primary btn-block mt-3"]]
            )
        ->getForm();

        $form->handleRequest($request);

        if($form->isSubmitted() && $form->isValid() ){
            $month = $form["month"]->getData();
            $year = $form["year"]->getData();
            $name = $form["name"]->getData();

            $date = new \DateTime("$month.01.$year");
            $timetable->setDate($date);
            $timetable->setName($name);

            $em = $this->getDoctrine()->getManager();
            $em->persist($timetable);
            $em->flush();

            return $this->redirect("/newtimetable");
        }

这是我的实体类:

    <?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;


/**
 * @ORM\Entity(repositoryClass="App\Repository\TimetableRepository")
 * @UniqueEntity("date",message="The date is not unique")
 *
 */

class Timetable
{

    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

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

    /**
     * @ORM\Column(type="date", unique=true, name="date")
     */
    private $date;

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

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

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

    public function setDate(\DateTimeInterface $date): self
    {
        $this->date = $date;

        return $this;
    }
}

基本上,我想创建一个具有唯一日期字段的 Timetable 对象。我没有收到任何错误,该表单可以正常工作并创建一个新条目。 我对其他字段尝试了同样的事情,但验证无论如何都不起作用。 有人可以给我一些关于如何让它发挥作用的提示吗? 谢谢。

【问题讨论】:

  • 您是否应该将您的实体对象作为参数传递给 createFormBuilder - 您是否得到与$form = $this-&gt;createFormBuilder($timetable); 相同的结果?
  • 那么我将无法创建带有“年”和“月”选择字段的表单,因为它们不是实际的实体字段。这就是为什么我没有将 $timetable 作为参数传递给 formBuilder。
  • 你是对的,问题出在参数上。如何验证不属于实体但最终成为单个实体字段的字段?
  • 尝试使用DateType 表单域 (docs)。 Symfony 可以为您处理月/年选择框。
  • 作为故障排除措施,请确保使用正确的 SQL 生成架构。你可以试试 bin/console dictionary:schema:create --dump-sql 并粘贴结果吗?

标签: php symfony entity


【解决方案1】:

这个答案可能为时已晚,但我们永远不知道......

在 UniqueEntity 的注释中,您缺少参数字段。所以它应该像这样工作:

* @UniqueEntity(fields="date", message="The date is not unique")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-13
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多