猿问

自动在CollectionType中设置数据库ID

我有两个实体,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;

    }


 

ibeautiful
浏览 130回答 1
1回答

qq_花开花谢_0

只需从ExceptionType*中删除字段,然后在中ParkingType,更改addException:public function addException(Exception $exception){&nbsp; &nbsp; $this->exceptions->add($exception);&nbsp; &nbsp; $exception->setType($this); // <-- this is new.}更新:还必须将exceptionsCollectionType选项设置by_reference为false,这样加法器实际上是由表单组件调用的。这是做到这一点的一种方法。另一个选择是在您的控制器中执行此操作,并调用setType您在ParkingType中找到的每个Exception ...*)这是假设您永远不会自己编辑Exception。否则,可以有条件地在某些选项上添加停放类型表单字段,或者ParkingTypeExceptionType对不添加停放类型表单字段的“例外”(例如)使用其他表单。
随时随地看视频慕课网APP
我要回答