Downloadable doctrine: loading multiple files on one object

I use the excellent doctrine extension, which can be downloaded. I can upload one file to an object just fine, but how can I upload two different files to the same object?

* @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 

On my controller, I:

 $uploadableManager->markEntityToUpload($article, $article->getPhoto()); $uploadableManager->markEntityToUpload($article, $article->getPdf()); 

Only the last file is downloaded and stored in the database. How can i do this?

+6
source share
1 answer

You are probably confused.

You have an Article object with two fields: photo and pdf, but no $ materia object. You should probably change $ materia to $ article. But this will not work, because @Uploadable cannot upload multiple files for the same object.

Hint: use VichUploaderBundle to upload files to Doctrine

UPD: Here is an example class.

 <?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 (ie not using Symfony Form) ensure an instance * of 'UploadedFile' is injected into this setter to trigger the update. If this * bundle 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 (ie not using Symfony Form) ensure an instance * of 'UploadedFile' is injected into this setter to trigger the update. If this * bundle 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; } } 

And you need to configure VichUploader like this:

 # 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 

Be careful. You may get confused in configuration, comparisons, methods ... just read the manual carefully and think. https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/usage.md

+2
source

All Articles