【发布时间】:2021-10-19 08:37:23
【问题描述】:
我有一个完美的多文件上传。使用一个“浏览”按钮一次上传一个文件。它基本上是一个可以包含许多图像的 Places 实体。
我正在尝试将其修改为仅通过一个“浏览”窗口一次上传所有文件。使用 Ctrl / shift 选择多个文件。
所以我得到的第一个内部信息是 VichUploader (VichFileType::class) 不支持多次上传,所以到目前为止我发现只有一个选择是在我的 AttachmentType.php 中将 VichFileType::class 更改为 FileType::class 并添加在选项['multiple' => true] 所以现在我在我的管理面板字段中可以一次选择多个文件。这正是我需要的。但是在我选择了所有需要的文件并单击Create 创建一个新位置后,我得到了错误:Return value of Vich\UploaderBundle\Mapping\PropertyMapping::getFile() must be an instance of Symfony\Component\HttpFoundation\File\File or null, array returned。似乎 VichUploader 只等待一个文件而不是数组,所以我修改了我的图像实体。
之前:
/**
* @param mixed $imageFile
*/
public function setImageFile($imageFile): void {
$this->imageFile = $imageFile;
if ($imageFile) {
$this->updatedAt = new \DateTime();
}
}
之后:
/**
* @param mixed $imageFile
*/
public function setImageFile($imageFile): void {
foreach ($imageFile as $file) {
$this->imageFile = $file;
if ($imageFile) {
$this->updatedAt = new \DateTime();
}
}
}
之后,错误消失了,但问题是如果我添加多张图片,则只添加数组中的最后一张。
完整代码:
Places.php
/**
* @ORM\OneToMany(targetEntity=Images::class, mappedBy="place", cascade={"persist", "remove"})
*/
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->setPlace($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->getPlace() === $this) {
$image->setPlace(null);
}
}
return $this;
}
Images.php
/**
* @ORM\Entity(repositoryClass=ImagesRepository::class)
* @Vich\Uploadable()
*/
class Images
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @Vich\UploadableField(mapping="attachments", fileNameProperty="title")
*/
private $imageFile;
/**
* @ORM\Column(type="datetime")
*/
private $updatedAt;
/**
* @ORM\ManyToOne(targetEntity=Places::class, inversedBy="images")
*/
private $place;
/**
* @ORM\ManyToOne(targetEntity=Regions::class, inversedBy="image")
*/
private $region;
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(?string $title): self
{
$this->title = $title;
return $this;
}
public function setUpdatedAt(\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* @return mixed
*/
public function getUpdatedAt() {
return $this->updatedAt;
}
/**
* @param mixed $imageFile
*/
public function setImageFile($imageFile): void {
foreach ($imageFile as $file) {
$this->imageFile = $file;
if ($imageFile) {
$this->updatedAt = new \DateTime();
}
}
}
/**
* @return mixed
*/
public function getImageFile() {
return $this->imageFile;
}
public function getPlace(): ?Places
{
return $this->place;
}
public function setPlace(?Places $place): self
{
$this->place = $place;
return $this;
}
public function getRegion(): ?Regions
{
return $this->region;
}
public function setRegion(?Regions $region): self
{
$this->region = $region;
return $this;
}
}
AttachmentType.php
class AttachmentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('imageFile', FileType::class, [
'multiple' => true
])
->add('updatedAt')
->add('place')
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Images::class,
]);
}
}
【问题讨论】:
-
嗨,@laneboyandrew!我建议你开始赏金 50 声望点。它可能会吸引知识渊博的用户。
标签: php symfony easyadmin vichuploaderbundle