我使用优秀的doctrine扩展程序上传。我可以为每个实体上传一个文件,但是如何在同一个实体上传两个不同的文件?
* @Gedmo\Uploadable(path="uploads/articles", appendNumber=true, filenameGenerator="SHA1")
class Article
{
* @ORM\Column(name="photo", type="string", length=255)
* @Gedmo\UploadableFilePath
private $photo
* @ORM\Column(name="pdf", type="string", length=255)
* @Gedmo\UploadableFilePath
private $pdf
在我的控制器上我有:
$uploadableManager->markEntityToUpload($article, $article->getPhoto());
$uploadableManager->markEntityToUpload($article, $article->getPdf());
仅上传最后一个文件并将其保存到数据库中。我怎么能这样做?
答案 0 :(得分:2)
你可能会感到困惑。
您的文章实体包含两个字段:照片和pdf,但没有$ materia实体。您可能应该将$ materia更改为$ article。但这不起作用,因为@Uploadable无法为同一个实体上传多个文件。
提示:使用VichUploaderBundle进行Doctrine文件上传处理
UPD:以下是示例类。
<?php
namespace Acme\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity
* @ORM\Table(name="article")
* @Vich\Uploadable
*/
class Article
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
// ..... other fields
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Vich\UploadableField(mapping="article_photo", fileNameProperty="photoName")
*
* @var File
*/
private $photoFile;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $photoName;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Vich\UploadableField(mapping="article_pdf", fileNameProperty="pdfName")
*
* @var File
*/
private $pdfFile;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $pdfName;
/**
* @ORM\Column(type="datetime")
*
* @var \DateTime
*/
private $updatedAt;
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* @param \DateTime $updatedAt
* @return Article
*/
public function setUpdatedAt(\DateTime $updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* 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 $photo
*
* @return Article
*/
public function setPhotoFile(File $photo = null)
{
$this->photoFile = $photo;
if ($photo) {
// 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 \DateTime('now');
}
return $this;
}
/**
* @return File
*/
public function getPhotoFile()
{
return $this->photoFile;
}
/**
* @param string $photoName
*
* @return Article
*/
public function setPhotoName($photoName)
{
$this->photoName = $photoName;
return $this;
}
/**
* @return string
*/
public function getPhotoName()
{
return $this->photoName;
}
/**
* 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 $pdf
*
* @return Article
*/
public function setPdfFile(File $pdf = null)
{
$this->pdfFile = $pdf;
if ($pdf) {
// 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 \DateTime('now');
}
return $this;
}
/**
* @return File
*/
public function getPdfFile()
{
return $this->pdfFile;
}
/**
* @param string $pdfName
*
* @return Article
*/
public function setPdfName($pdfName)
{
$this->pdfName = $pdfName;
return $this;
}
/**
* @return string
*/
public function getPdfName()
{
return $this->pdfName;
}
}
你需要以这种方式配置VichUploader:
# app/config/config.yml
vich_uploader:
db_driver: orm
mappings:
article_photo:
uri_prefix: /images/articles/photos
upload_destination: %kernel.root_dir%/../web/images/articles/photos
article_pdf:
uri_prefix: /images/articles/pdfs
upload_destination: %kernel.root_dir%/../web/images/articles/pdfs
要注意。您可能会对配置,映射,方法感到困惑......只需仔细阅读手册即可。 https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/usage.md