我有两个实体,ParkingType和Exception,它们之间存在一个OneToMany关系,因为每个ParkingType可以具有多个异常。
我做到了,因此每当我创建一个新的ParkingType时,我也可以同时创建与其相关的异常。我通过使用一个包含在parkingtype表单内的异常表单的CollectionType做到了这一点。集合类型是动态的,因此我可以添加任意多个异常。
问题:例外表中有一列名为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;
}
}
ParkingType实体:
<?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,
]);
}
}
答案 0 :(得分:1)
只需从ExceptionType
*中删除字段,然后在您的ParkingType
中更改addException
:
public function addException(Exception $exception)
{
$this->exceptions->add($exception);
$exception->setType($this); // <-- this is new.
}
更新:您还必须将例外CollectionType
选项by_reference
设置为false
,这样加法器实际上是由表单组件调用的。 / p>
这是一种方法。另一个选择是在您的控制器中执行此操作,并对在ParkingType中发现的每个异常调用setType
。
*)这是假设,您永远不会自己编辑Exception。否则,要么有条件地在某些选项上添加停放类型表单字段,要么对不添加停放类型表单字段的异常使用其他表单(例如,名为ParkingTypeExceptionType
)。