我正在尝试按照本教程进行操作 https://symfony.com/doc/current/controller/upload_file.html 它很好而且有效,但我需要上传多个文件。
现在我有 2 个相关的表产品和产品图片。 我知道如何在控制器中上传多个文件,但我想要它在理论上,我认为它更干净和专业。
所以我尝试了很多与foreach的组合,但总是有一个错误:
类型为"Web\BaseBundle\Entity\ProductImages"的预期参数, 给出"Symfony\Component\HttpFoundation\File\UploadedFile">
我的实体:
产品:
class Product
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
///
/**
* @ORMOneToMany(targetEntity="WebBaseBundleEntityProductImages", mappedBy="product", cascade={"persist", "remove"})
* @ORMOrderBy({"id" = "ASC"})
*
*/
private $images;
public function __toString(): string
{
return $this->getTitle();
}
/**
* Constructor
*/
public function __construct()
{
$this->images = new DoctrineCommonCollectionsArrayCollection();
}
////
/**
* Add image
*
* @param WebBaseBundleEntityProductImages $image
*
* @return Product
*/
public function addImage(WebBaseBundleEntityProductImages $image)
{
$this->images[] = $image;
return $this;
}
/**
* Remove image
*
* @param WebBaseBundleEntityProductImages $image
*/
public function removeImage(WebBaseBundleEntityProductImages $image)
{
$this->images->removeElement($image);
}
/**
* Get images
*
* @return DoctrineCommonCollectionsCollection
*/
public function getImages()
{
return $this->images;
}
}
产品图片:
class ProductImages
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* @ORMColumn(name="image", type="string", length=255, nullable=true)
* @AssertNotBlank(message="Please, upload the product brochure as a PDF file.")
* @AssertFile(mimeTypes={ "image/jpeg", "image/jpg", "image/png" })
*/
private $image;
///
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
///
/**
* Set image
*
* @param string $image
*
* @return ProductImages
*/
public function setImage($image)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* @return string
*/
public function getImage()
{
return $this->image;
}
}
文件上传器:
class FileUploader
{
private $targetDir;
public function __construct($targetDir)
{
$this->targetDir = $targetDir;
}
public function upload(UploadedFile $file)
{
$fileName = md5(uniqid()).'.'.$file->guessExtension();
$file->move($this->getTargetDir(), $fileName);
return $fileName;
}
public function getTargetDir()
{
return $this->targetDir;
}
}
文件上传侦听器:
class FileUploadListener
{
private $uploader;
public function __construct(FileUploader $uploader)
{
$this->uploader = $uploader;
}
public function prePersist(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
$this->uploadFile($entity);
}
public function preUpdate(PreUpdateEventArgs $args)
{
$entity = $args->getEntity();
$this->uploadFile($entity);
}
private function uploadFile($entity)
{
//Tried a lot of combinations with foreach, but not working :(
if (!$entity instanceof Product) {
return;
}
$file = $entity->getImages();
// only upload new files
if (!$file instanceof UploadedFile) {
return;
}
$fileName = $this->uploader->upload($file);
$entity->setImages($fileName);
}
}
有人可以帮助我吗? 谢谢
由我自己修复...
产品实体中文件的声明临时变量
private $files;
public function __construct()
{
$this->files = new DoctrineCommonCollectionsArrayCollection();
}
public function setFiles($files)
{
$this->files = $files;
return $this;
}
public function getFiles()
{
return $this->files;
}
更新上传文件功能:
private function uploadFile($entity)
{
if (!$entity instanceof Product) {
return;
}
$files = $entity->getFiles();
foreach ($files as $file) {
if (!$file instanceof UploadedFile) {
return;
}
$fileName = $this->uploader->upload($file);
$productImage = new ProductImages();
$productImage->setImage($fileName);
$productImage->setProduct($entity);
$entity->addImage($productImage);
}
}