Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine uploadable: Multiple file upload on the same entity

I'm using the excellent doctrine extension uploadable. I can upload one file per entity just fine, but how can I upload two different files on the same entity?

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

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

Only the last file is uploaded and saved to the database. How can I do this?

like image 234
Diego Castro Avatar asked Mar 24 '14 18:03

Diego Castro


1 Answers

You probably confused something.

You have Article entity with two fields: photo and pdf, but there is no $materia entity. You probably should change $materia to $article. But this won't work because @Uploadable cannot upload multiple files for the same entity.

Hint: use VichUploaderBundle for Doctrine file uploads handling

UPD: Here is 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 (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;
    }
}

And you need to configure VichUploader this way:

# 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 attentive. You can get confused with configuration, mappings, methods... just read manual carefully and thoughtly. https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/usage.md

like image 144
Serhii Smirnov Avatar answered Sep 30 '22 19:09

Serhii Smirnov