在集合类型中自动设置数据库 ID



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

这样做是每当我创建一个新的 ParkingType 时,我也可以同时创建与之相关的异常。我通过使用在停车类型表单中包含异常表单的 CollectionType 来做到这一点。集合类型是动态的,因此我可以根据需要添加任意数量的异常。

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


<?php
namespace AppEntity;
use DoctrineORMMapping as ORM;
/**
 * @ORMEntity(repositoryClass="AppRepositoryExceptionRepository")
 */
class Exception
{
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     */
    private $id;
    /**
     * @ORMColumn(type="string", length=200, nullable=true)
     */
    private $nom;
    /**
     * @ORMColumn(type="date", nullable=true)
     */
    private $datedebut;
    /**
     * @ORMColumn(type="date", nullable=true)
     */
    private $datefin;
    /**
     * @ORMColumn(type="time", nullable=true)
     */
    private $tempsdebut;
    /**
     * @ORMColumn(type="time", nullable=true)
     */
    private $tempsfin;
    /**
     * @ORMManyToOne(targetEntity="AppEntityTypeParking", inversedBy="exceptions")
     * @ORMJoinColumn(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 AppEntity;
/**
 * @ORMEntity(repositoryClass="AppRepositoryTypeParkingRepository")
 */
class TypeParking
{
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     */
    private $id;
    /**
     * @ORMColumn(type="string", length=55)
     */
    private $libelle;
    /**
     * @ORMColumn(type="time", nullable=true)
     */
    private $tempsmax;
    /**
     * @ORMColumn(type="date", nullable=true)
     */
    private $jourdebut;
    /**
     * @ORMColumn(type="date", nullable=true)
     */
    private $jourfin;

    /**
     * @ORMOneToMany(targetEntity="AppEntityException", 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);
    }
}

停车场类型表格:

<?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.
}

更新:您还必须将选项by_reference CollectionType例外设置为 false ,以便 adder 实际上由表单组件调用。

另一种选择是在控制器中执行此操作,并为在停车类型中找到的每个异常调用setType...

*( 这假设您永远不会自行编辑异常。否则,请有条件地在某些选项上添加停车类型表单域,或者对例外使用不同的表单(例如名为 ParkingTypeExceptionType (,该表单不会为停车类型添加表单字段。

相关内容

  • 没有找到相关文章

最新更新