Symfony 4 在 EntityType 中添加 4 个输入类型文件

Symfony 4 add 4 input type file in EntityType

我有实体 Boisson 与实体 Image oneToMany 我必须为 Boisson

添加 4 个图像

实体Boisson

/**
 * @ORM\OneToMany(targetEntity="App\Entity\Image", mappedBy="boisson")
 */
private $images;

/**
 * @return Collection|Image[]
 */
public function getImages(): Collection
{
    return $this->images;
}

public function addImage(Image $image): self
{
    if (!$this->images->contains($image)) {
        $this->images[] = $image;
        $image->setBoisson($this);
    }

    return $this;
}

public function removeImage(Image $image): self
{
    if ($this->images->contains($image)) {
        $this->images->removeElement($image);
        // set the owning side to null (unless already changed)
        if ($image->getBoisson() === $this) {
            $image->setBoisson(null);
        }
    }

    return $this;
}

我的BoissonType

结束
$builder
    ->add('user',EntityType::class, [
        'class' => User::class,
        'query_builder' => function (UserRepository $er) {
            return $er->createQueryBuilder('u');
        },
        'choice_label' => 'username'])
    ->add('nom')
    ->add('prix')
;

如何获得 4 个输入类型文件以将我的图像添加到我的 boisson

嵌入表单时,如果您有一对多关系,'many' 意味着您必须在 'one' Boisson 表单中使用 CollectionType,如下所示。

$builder->add('images', CollectionType::class, [
    'entry_type' => ImageType::class,

]);

查看文档here:了解更多关于此类字段配置的信息。