简体   繁体   中英

Nested form symfony 3

I have 2 entity in my symfony application with ManyToOne Relationship

class Sprint
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;...

And

Class Livrable
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @ORM\ManyToOne(targetEntity="App\MemberBundle\Entity\Sprint", cascade={"persist"})
     * @ORM\JoinColumn(nullable=true)
     */
    private $sprint; ....

And in my form LivrableType, I have

   public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name')
                ->add('sprint', SprintType::class);
    }

Everything goes well when I create a Livrable with a sprint but, what I need is to allow the user to either choose his sprint if existing and create it if not existing. Thanks you for your help

Instead of mapping form directly to entity, make some kind of DTO (LivrableDTO), which will have Sprint, SprintSelect and SprintCreate properties. This DTO you can map back to your Livrable entity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM