我正在尝试按照本教程https://symfony.com/doc/current/controller/upload_file.html进行操作,并且它很好用,但我需要上传多个文件。
现在我有2个相关的表产品和ProductImages。 我知道如何在控制器中上传多个文件,但我希望它在学说中,我认为它更干净,更专业。
所以我尝试了很多与foreach的组合,但总是出错:
类型" Web \ BaseBundle \ Entity \ ProductImages"的预期参数, " Symfony的\元器件\ HttpFoundation \文件\ UploadedFile的"给定
我的实体:
产品:
import pandas as pd
df = pd.DataFrame.from_csv('text.csv',index_col=None,header=None)
df[0] = df[0].apply(lambda x:x.strip())
df = df.groupby(0).sum().reset_index()
mylist = zip(df[0],map(lambda x: '${:.2f}'.format(x),df[1]))
ProductImages:
class Product
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
///
/**
* @ORM\OneToMany(targetEntity="Web\BaseBundle\Entity\ProductImages", mappedBy="product", cascade={"persist", "remove"})
* @ORM\OrderBy({"id" = "ASC"})
*
*/
private $images;
public function __toString(): string
{
return $this->getTitle();
}
/**
* Constructor
*/
public function __construct()
{
$this->images = new \Doctrine\Common\Collections\ArrayCollection();
}
////
/**
* Add image
*
* @param \Web\BaseBundle\Entity\ProductImages $image
*
* @return Product
*/
public function addImage(\Web\BaseBundle\Entity\ProductImages $image)
{
$this->images[] = $image;
return $this;
}
/**
* Remove image
*
* @param \Web\BaseBundle\Entity\ProductImages $image
*/
public function removeImage(\Web\BaseBundle\Entity\ProductImages $image)
{
$this->images->removeElement($image);
}
/**
* Get images
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getImages()
{
return $this->images;
}
}
FileUploader:
class ProductImages
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* @ORM\Column(name="image", type="string", length=255, nullable=true)
* @Assert\NotBlank(message="Please, upload the product brochure as a PDF file.")
* @Assert\File(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;
}
}
FileUploadListener:
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;
}
}
有人可以帮我吗? 感谢
答案 0 :(得分:1)
由我自己修复......
宣布临时。产品实体
中文件的变量private $files;
public function __construct()
{
$this->files = new \Doctrine\Common\Collections\ArrayCollection();
}
public function setFiles($files)
{
$this->files = $files;
return $this;
}
public function getFiles()
{
return $this->files;
}
更新了uploadFile函数:
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);
}
}