在 CollectionType 中自动设置数据库 ID

automatically set database ID in CollectionType

我有两个实体,ParkingType 和 Exception,它们之间存在 OneToMany 关系,因为每个 ParkingType 可以有多个异常。

我做到了,所以每当我创建一个新的 ParkingType 时,我也可以同时创建与之相关的异常。我通过在 parkingtype 表单中使用包含异常表单的 CollectionType 来做到这一点。 collectiontype 是动态的,所以我可以添加任意数量的例外。

问题:异常 table 有一个名为 type_id 的列,用于将该异常与 ParkingType 相关联,我必须填充它每次都通过从下拉列表中进行选择来让自己参与进来。我不想那样做,我希望该字段默认引用我刚刚创建的对象。 我的代码: 异常实体:


<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

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

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

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

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

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

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

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

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

    public function getNom(): ?string
    {
        return $this->nom;
    }

    public function setNom(?string $nom): self
    {
        $this->nom = $nom;

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

    public function getType(): ?TypeParking
    {
        return $this->type;
    }

    public function setType(?TypeParking $type): self
    {
        $this->type = $type;

        return $this;
    }
}

停车类型实体:

<?php

namespace App\Entity;

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

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

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

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

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



    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Exception", mappedBy="type", cascade={"persist"})
     */
    private $exceptions;

    public function __construct()
    {
        $this->exceptions = new ArrayCollection();
    }
    public function getId(): ?int
    {
        return $this->id;
    }

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

    public function setTempsmax(\DateTimeInterface $tempsmax): self
    {
        $this->tempsmax = $tempsmax;

        return $this;
    }

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

    public function setJourdebut(\DateTimeInterface $jourdebut): self
    {
        $this->jourdebut = $jourdebut;

        return $this;
    }

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

    public function setJourfin(\DateTimeInterface $jourfin): self
    {
        $this->jourfin = $jourfin;

        return $this;
    }



    public function getLibelle(): ?string
    {
        return $this->libelle;
    }

    public function setLibelle(string $libelle): self
    {
        $this->libelle = $libelle;

        return $this;
    }

    /**
     * @return Collection|Exception[]
     */
    public function getExceptions(): Collection
    {
        return $this->exceptions;
    }

    public function removeException(Exception $exception): self
    {
        if ($this->exceptions->contains($exception)) {
            $this->exceptions->removeElement($exception);
            // set the owning side to null (unless already changed)
            if ($exception->getType() === $this) {
                $exception->setType(null);
            }
        }

        return $this;
    }
    public function addException(Exception $exception)
    {
        $this->exceptions->add($exception);
    }

}

ParkingType 形式:

<?php


class TypeParkingType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('libelle')
            ->add('tempsmax')
            ->add('jourdebut')
            ->add('jourfin')





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


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

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

例外表格:

class ExceptionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder            

        ->add('nom')
        ->add('datedebut')
        ->add('datefin')
        ->add('tempsdebut')
        ->add('tempsfin')
        ->add('type',EntityType::class, [
            'class' => TypeParking::class,
            'choice_label' => 'libelle',
        ])        

 ;
    }

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

只需从 ExceptionType* 中删除该字段,然后在您的 ParkingType 中更改 addException:

public function addException(Exception $exception)
{
    $this->exceptions->add($exception);
    $exception->setType($this); // <-- this is new.
}

update:您还必须将例外 CollectionType 选项 by_reference 设置为 false,这样加法器实际上被调用表单组件。

这是一种方法。另一种选择是在您的控制器中执行此操作,并为您在 ParkingType 中找到的每个异常调用 setType ...

*) 这假定您永远不会自行编辑异常。否则,要么有条件地在某些选项上添加停车类型表单字段,要么使用不为停车类型添加表单字段的例外情况(例如名为 ParkingTypeExceptionType)的不同表单。