2014-03-24 4 views
6

Ich benutze die exzellente Doktrin Erweiterung hochladbar. Ich kann eine Datei pro Entity hochladen, aber wie kann ich zwei verschiedene Dateien auf dieselbe Entity hochladen?Doctrine uploadable: Mehrere Dateien hochladen auf derselben Entität

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

Auf meinem Controller ich habe:

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

Nur die letzte Datei hochgeladen und in der Datenbank gespeichert. Wie kann ich das machen?

Antwort

2

Sie haben wahrscheinlich etwas verwirrt.

Sie haben Artikelentität mit zwei Feldern: Foto und pdf, aber es gibt keine $ materia Entität. Sie sollten wahrscheinlich $ Materia zu $ ​​Artikel ändern. Dies funktioniert jedoch nicht, da @Uploadable nicht mehrere Dateien für dieselbe Entität hochladen kann.

Hinweis: VichUploaderBundle für Lehre Dateiuploads verwenden

UPD Handhabung: Hier Beispielklasse ist.

<?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; 
    } 
} 

Und brauchen Sie VichUploader auf diese Weise zu konfigurieren:

# 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 

Seien Sie aufmerksam. Sie können verwirrt werden mit Konfiguration, Mappings, Methoden ... lesen Sie einfach manuell und durchdacht. https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/usage.md

+1

Mein schlechtes, reparierte den Tippfehler. Am Ende habe ich eine Photo-Entity erstellt, um mein Problem zu lösen. –

+1

Konnten Sie Ihren Code für diese Lösung @DiegoCastro veröffentlichen? – webDEVILopers

+0

Ich konnte das nicht lösen. Ich beendete die Erstellung einer Entität ArticlePhoto und ArticlePdf. –

Verwandte Themen