Vich 上传,多张上传图片不起作用

Vich upload, multi upload images doesn't work

我正在尝试使用 vich 上传包和 symfony 5 上传多张图片:

我在用户和图像之间创建了一对多关系:

/**
 * @ORM\Entity(repositoryClass=UserRepository::class)
 * @UniqueEntity(fields={"email"}, message="There is already an account with this email")
 */
class User implements UserInterface
{

...

/**
 * @ORM\OneToMany(targetEntity=Images::class, mappedBy="owner", cascade={"persist"})
 */
private $images;

 public function __construct()
{
    ...
    $this->images = new ArrayCollection();
}

...

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

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

    return $this;
}

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

    return $this;
}

我的图片实体可以上传:

/**
 * @ORM\Entity(repositoryClass=ImagesRepository::class)
 * @Vich\Uploadable
 */
class Images
{
    
     ..

    /**
     * @Vich\UploadableField(mapping="user_images", fileNameProperty="imageName", size="imageSize")
     * @var File|null
     */
    private $imageFile;

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

    /**
     * @ORM\Column(type="integer")
     *
     * @var int|null
     */
    private $imageSize;

    /**
     * @ORM\Column(type="datetime")
     *
     * @var \DateTimeInterface|null
     */
    private $updatedAt;

    

    public function getImageFile(): ?File
    {
        return $this->imageFile;
    }

    /**
     * @param File|UploadedFile|null $imageFile
     */
    public function setImageFile(?File $imageFile = null): void
    {
        $this->imageFile = $imageFile;

        if (null !== $imageFile) {
            // It is required that at least one field changes if you are using doctrine
            // otherwise the event listeners won't be called and the file is lost
            $this->updatedAt = new \DateTimeImmutable();
        }
    }

    public function getImageName(): ?string
    {
        return $this->imageName;
    }

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

    public function getImageSize(): ?int
    {
        return $this->imageSize;
    }

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

    public function getUpdatedAt(): ?\DateTimeInterface
    {
        return $this->updatedAt;
    }

    public function setUpdatedAt(\DateTimeInterface $updatedAt): self
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }
}

我在用户类型中添加了:

  ->add(
                'images',
                CollectionType::class,
                array(
                    'entry_type' => ImagesType::class,
                    'allow_add'    => true,
                    'allow_delete' => true,
                )
            )

在我的图像类型中: ->添加( 'imageFile', FileType::class ) ;

最后在我的用户表单 twig 中:

 <div class="form-group">
                                    <div class="custom-file">
                                        {{  form_widget(form.images, { 'attr' : { 'class' : 'custom-file-input' } })  }}
                                        {{ form_label(form.images, 'Choose images', {'label_attr': {'class': 'custom-file-label'}}) }}
                                    </div>
                                </div>
                            </div>

我得到了这样的输入,但它不起作用:

您应该使用 VichFileType 类型而不是 FileType 类型,如捆绑文档中所述: https://github.com/dustin10/VichUploaderBundle/blob/master/docs/form/vich_file_type.md

代码示例(来自文档):

use Vich\UploaderBundle\Form\Type\VichFileType;

class Form extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        // ...

        $builder->add('genericFile', VichFileType::class, [
            'required' => false,
            'allow_delete' => true,
            'delete_label' => '...',
            'download_uri' => '...',
            'download_label' => '...',
            'asset_helper' => true,
        ]);
    }
}

这是解决方案的一部分,现在 images 是一个集合,所以你需要有一个 VichFileType 的集合:https://symfony.com/doc/current/form/form_collections.html