我有兩個實體,ParkingType和Exception,它們之間存在一個OneToMany關(guān)系,因為每個ParkingType可以具有多個異常。我做到了,所以每當我創(chuàng)建一個新的ParkingType時,我也可以同時創(chuàng)建與其相關(guān)的異常。我通過使用一個包含在parkingtype表單內(nèi)的異常表單的CollectionType做到了這一點。集合類型是動態(tài)的,因此我可以根據(jù)需要添加盡可能多的異常。問題:異常表中有一列名為type_id的列,該列用于將該異常與ParkingType相關(guān)聯(lián),我每次必須通過從下拉列表中進行選擇來自己填充該字段。我不想這樣做,我希望該字段引用默認情況下剛創(chuàng)建的對象。我的代碼:異常實體:<?phpnamespace 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; }
自動在CollectionType中設(shè)置數(shù)據(jù)庫ID
ibeautiful
2021-05-03 14:15:33