php - Symfony 4 - VichUploaderBundle - 文件输入未呈现
问题描述
我有一个与我的实体“照片”相关的 OneToMany 实体“FicheProduit”,因为产品表可以有多张照片。在我的表单中,我想通过 VichUploaderBundle 创建多个上传,但我收到此错误:
函数 Vich\UploaderBundle\Form\Type\VichImageType::__construct() 的参数太少,在第 92 行的 C:\wamp64\www\blh_wines\vendor\symfony\form\FormRegistry.php 中传递了 0,预计至少 3
FicheProduit.php:
<?php
declare(strict_types = 1);
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Class FicheProduit
*
* @package App\Entity
*
* @ORM\Entity
*/
class FicheProduit
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer", options={"unsigned":true}, nullable=false)
*
* @var int
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="Photo", mappedBy="ficheProduit", cascade={"persist"})
*
* @Assert\Valid()
*
* @var Photo[]|ArrayCollection
*/
protected $photos;
/**
* FicheProduit constructor.
*/
public function __construct()
{
$this->photos = new ArrayCollection();
}
/**
* @return int
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @param int $id
*/
public function setId(int $id): void
{
$this->id = $id;
}
/**
* @return Photo[]|ArrayCollection
*/
public function getPhotos()
{
return $this->photos;
}
/**
* @param Photo[]|ArrayCollection $photos
*/
public function setPhotos($photos): void
{
$this->photos = $photos;
}
/**
* Add photo
*
* @param Photo $photo
*
* @return FicheProduit
*/
public function addPhoto(Photo $photo)
{
$photo->setProduct($this);
$this->photos[] = $photo;
return $this;
}
/**
* Remove photo
*
* @param Photo $photo
*/
public function removePhoto(Photo $photo)
{
$this->photos->removeElement($photo);
}
}
照片.php
<?php
declare(strict_types = 1);
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity
* @Vich\Uploadable
*/
class Photo
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @Vich\UploadableField(mapping="fiche_produit_photo", fileNameProperty="imageName", size="imageSize")
*
* @var File
*/
protected $imageFile;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
protected $imageName;
/**
* @ORM\Column(type="integer")
*
* @var integer
*/
protected $imageSize;
/**
* @ORM\Column(type="datetime")
*
* @var \DateTime
*/
protected $updatedAt;
/**
* @ORM\ManyToOne(targetEntity="FicheProduit", inversedBy="photos")
*/
protected $ficheProduit;
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
*/
public function setImageFile(?File $image = null): void
{
$this->imageFile = $image;
if (null !== $image) {
// 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();
}
}
/**
* @return null|File
*/
public function getImageFile(): ?File
{
return $this->imageFile;
}
/**
* @param null|string $imageName
*/
public function setImageName(?string $imageName): void
{
$this->imageName = $imageName;
}
/**
* @return null|string
*/
public function getImageName(): ?string
{
return $this->imageName;
}
/**
* @param int|null $imageSize
*/
public function setImageSize(?int $imageSize): void
{
$this->imageSize = $imageSize;
}
/**
* @return int|null
*/
public function getImageSize(): ?int
{
return $this->imageSize;
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id): void
{
$this->id = $id;
}
/**
* @return \DateTime
*/
public function getUpdatedAt(): ?\DateTime
{
return $this->updatedAt;
}
/**
* @param \DateTime $updatedAt
*/
public function setUpdatedAt(\DateTime $updatedAt): void
{
$this->updatedAt = $updatedAt;
}
/**
* @return mixed
*/
public function getFicheProduit()
{
return $this->ficheProduit;
}
/**
* @param mixed $ficheProduit
*/
public function setFicheProduit($ficheProduit): void
{
$this->ficheProduit = $ficheProduit;
}
}
vich_uploader.yaml :
vich_uploader:
db_driver: orm
mappings:
fiche_produit_photo:
uri_prefix: /images/products
upload_destination: '%kernel.project_dir%/public/images/products'
inject_on_load: false
delete_on_update: true
delete_on_remove: true
easy_admin.yaml :
easy_admin:
entities:
FicheProduit:
class: App\Entity\FicheProduit
list:
title: 'Fiche produit'
form:
title: 'Ajouter une fiche produit'
fields:
- { property: 'photos', type: 'collection', type_options: { entry_type: 'App\Form\PhotoType', by_reference: false }}
照片类型.php:
<?php
declare(strict_types = 1);
namespace App\Form;
use App\Entity\Photo;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Vich\UploaderBundle\Form\Type\VichImageType;
/**
* Class PhotoType
*
* @package App\Form
*/
class PhotoType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('imageName')
->add('imageFile', VichImageType::class, [
'required' => false,
'allow_delete' => true,
'download_label' => '...',
'download_uri' => true,
'image_uri' => true,
'imagine_pattern' => '...',
])
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Photo::class,
]);
}
}
我试图替换这个:
->add('imageFile', VichImageType::class, [
'required' => false,
'allow_delete' => true,
'download_label' => '...',
'download_uri' => true,
'image_uri' => true,
'imagine_pattern' => '...',
])
这样 :
->add('imageFile', CollectionType::class, ['entry_type' => VichImageType::class])
我没有更多错误,但我的文件输入没有显示,并且我旁边有一个“0”。
我遵循了这个文档,但它对我没有多大帮助:
https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/form/vich_image_type.md
https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/usage.md
解决方案
好吧,我自己从未使用过 EasyAdmin,但我认为您需要对 Photo 实体进行管理员定义:
easy_admin:
entities:
Photo:
# ...
form:
fields:
- { property: 'imageFile', type: 'vich_image' }
我认为您不需要 EasyAdmin 界面的 PhotoType formType。
如果您遵循本教程:https ://symfony.com/doc/master/bundles/EasyAdminBundle/integration/vichuploaderbundle.html#uploading-the-images-in-the-edit-and-new-views 您会看到有示例中没有 FormType。
推荐阅读
- ssas - 无法在 SSAS 钻取操作 SSAS 2012 中执行 ASSP 过程
- java - 为什么将字符“�”保存到文件中会保存为“?”?
- ios - UIContextualAction 不在 iOS 12.4 上显示图像,该代码适用于 iOS 13.4
- python - python - 使用索引的嵌套列表切片嵌套列表
- pandas-groupby - pandas groupby 尝试优化几个步骤
- c# - 无法在 IIS 托管的 .net web api 项目中运行 powershell 脚本
- python-3.x - 如何删除 tkinter 文本中的通配符?
- oop - 观察者模式:同一实例多次注册的情况
- python - 使用 Numpy 在数组中找到彼此之间足够距离的最大点的有效方法
- android - 使用 API 级别 15,我的 pendingIntent 不会在 taskStack 中发送额外的顶级意图