如何解决Vich 上传,多张上传图片不起作用
我正在尝试使用 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;
}
}
在 UserType 中我添加了:
->add(
'images',CollectionType::class,array(
'entry_type' => ImagesType::class,'allow_add' => true,'allow_delete' => true,)
)
在我的 ImagesType 中: ->添加( '图像文件', 文件类型::类 ) ;
最后在我的用户表单中:
<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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。