如何使用 EasyAdmin 在单个表单中创建具有多个日期的实体

How to create an entity with several dates in a single form with EasyAdmin

我正在尝试开发我的第一个 symfony 网络应用程序,我决定使用 EasyAdmin 包。

在这个网络应用程序中,我想定义以下模型:一个 Event 有几个日期。

为了创建这个,我在 symfony 控制台的帮助下创建了 2 个实体:EventEventDate:

<?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\EventRepository")
 */
class Event
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

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

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\EventDate", mappedBy="event", orphanRemoval=true)
     */
    private $dates;

    public function __construct()
    {
        $this->dates = new ArrayCollection();
    }

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

    /**
     * @return Collection|EventDate[]
     */
    public function getDates(): Collection
    {
        return $this->dates;
    }

    public function addDate(EventDate $date): self
    {
        if (!$this->dates->contains($date)) {
            $this->dates[] = $date;
            $date->setEvent($this);
        }

        return $this;
    }

    public function removeDate(EventDate $date): self
    {
        if ($this->dates->contains($date)) {
            $this->dates->removeElement($date);
            // set the owning side to null (unless already changed)
            if ($date->getEvent() === $this) {
                $date->setEvent(null);
            }
        }

        return $this;
    }

    public function __toString(): String
    {
        return $this->name;
    }

}
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

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

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

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

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

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

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

        return $this;
    }

    public function getEvent(): ?Event
    {
        return $this->event;
    }

    public function setEvent(?Event $event): self
    {
        $this->event = $event;

        return $this;
    }
}

为了方便用户,我想 "customize" 一个 Event 的形式,以允许用户以相同的形式创建事件及其日期。

为了做到这一点,我定义了 Event 实体的形式:

easy_admin:
  entities:
    Event:
      class: App\Entity\Event
      form:
        fields:
          - {property: 'name'}
          - {property: 'dates', type: 'collection'}

集合的呈现是正确的,因为我可以添加或删除日期:

但是如您所见,代表 EventDate 的字段是一个编辑文本。我认为这是因为该字段代表 EventDate class 而不仅仅是 date 属性。

目的是让我在 EasyAdmin 中添加新的 EventDate 时拥有日期选择器:

所以问题是:如何自定义 EasyAdmin 以便以单一形式添加 Event 及其日期?

感谢您的帮助!

我找到了方法。

我需要修改我的 yaml EasyAdmin 文件以引入 entry_type:

easy_admin:
  entities:
    Event:
      class: App\Entity\Event
      form:
        fields:
          - {property: 'name'}
          - {property: 'dates', type: 'collection', type_options: {entry_type: 'App\Form\EventDateForm', by_reference: false}}

然后,我必须创建 EventDateForm class:

<?php

namespace App\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class EventDateForm extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('date');
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'App\Entity\EventDate'
        ));
    }

}

我还需要像这样更新 Event 实体的 $date 属性:

/**
 * @ORM\OneToMany(targetEntity="App\Entity\EventDate", mappedBy="event", orphanRemoval=true, cascade={"persist", "remove"})
*/
private $dates;

渲染不是很漂亮,但可以: