以Symfony格式添加EntityType的CollectionType

时间:2019-05-23 19:27:31

标签: php symfony

我有一个Symfony 4项目,我想在我的表单中创建EntityType的CollectionType,这有可能吗?

通常我知道如何做一个CollectionType,但是这种情况很特殊。

我的安装实体有:

/**
 * @ORM\OneToMany(targetEntity="Option", mappedBy="installation", cascade={"persist"})
 *
 * @Assert\Valid()
 *
 * @var ArrayCollection|Option[]
 */
protected $options;

选项实体:

<?php

declare(strict_types = 1);

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 */
class Option
{
   /**
    * @ORM\Id()
    * @ORM\GeneratedValue()
    * @ORM\Column(type="integer", options={"unsigned": true})
    *
    * @var integer|null
    */
    protected $id;

    /**
     * @ORM\Column(type="string", length=255)
     *
     * @Assert\Type(type="string", groups={"etape_installation"})
     * @Assert\Length(max="255", groups={"etape_installation"})
     * @Assert\NotBlank(groups={"etape_installation"})
     *
     * @var string|null
     */
    protected $libelle;

    /**
     * @ORM\Column(type="integer", options={"unsigned": true})
     *
     * @Assert\Type(type="int")
     * @Assert\Range(min="1")
     * @Assert\NotBlank()
     *
     * @var integer|null
     */
    protected $prix;

    /**
     * @ORM\ManyToOne(targetEntity="Installation", inversedBy="options")
     *
     * @Assert\Valid()
     *
     * @var Installation|null
     */
    protected $installation;

    public function getLibelle(): ?string
    {
        return $this->libelle;
    }

    public function setLibelle(?string $libelle): void
    {
        $this->libelle = $libelle;
    }

    public function getPrix(): ?int
    {
        return $this->prix;
    }

    public function setPrix(?int $prix): void
    {
        $this->prix = $prix;
    }

    public function getInstallation(): ?Installation
    {
        return $this->installation;
    }

    public function setInstallation(?Installation $installation): void
    {
        $this->installation = $installation;
    }

    public function __toString(): ?string
    {
        return $this->getLibelle() . ' +' . $this->getPrix() . '€';
    }
}

InstallationType:

->add('options', CollectionType::class, [
    'entry_type'    => OptionType::class,
    'entry_options' => [
        'label' => 'Options supplémentaires',
    ],
    'allow_add'     => true,
    'by_reference'  => false,
    'allow_delete'  => true,
    'prototype'     => true,
])

Option entity

我被困在我的OptionType中。 我该怎么办?

提前谢谢

1 个答案:

答案 0 :(得分:0)

我建议在这里阅读symfony文档: https://symfony.com/doc/current/form/form_collections.html

相关问题