2013-07-05 4 views
1

Я пытаюсь загрузить файлы с помощью Doctrine в Symfony2. Я выполнил все шаги этого урока tutorial, но когда я отправляю свою форму, я не получаю ошибок, но файл не загружается, а путь в таблице «документ» равен нулю.Загрузка файлов с помощью Doctrine

Это моя сущность

<?php 

namespace projet\ClasseBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\Validator\Constraints as Assert; 

/** 
* Projet 
* 
* @ORM\Table() 
* @ORM\Entity(repositoryClass="projet\ClasseBundle\Entity\ProjetRepository") 
* @ORM\HasLifecycleCallbacks 
*/ 
class Projet 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="idMembre", type="integer") 
    */ 
    private $idMembre; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="idAssociation", type="integer") 
    */ 
    private $idAssociation; 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="StatutProjet", type="integer") 
    */ 
    private $StatutProjet; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="nomProjet", type="string", length=255) 
    */ 
    private $nomProjet; 

    /** 
    * @var boolean 
    * 
    * @ORM\Column(name="visibilite", type="boolean") 
    */ 
    private $visibilite; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="dateDebut", type="string", length=255) 
    */ 
    private $dateDebut; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="dateFin", type="string", length=255) 
    */ 
    private $dateFin; 


    /** 
    * @var float 
    * 
    * @ORM\Column(name="budgetActuel", type="float", nullable=true) 
    */ 
    private $budgetActuel; 


    /** 
    * @var float 
    * 
    * @ORM\Column(name="budget", type="float") 
    */ 
    private $budget; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="description", type="text") 
    */ 
    private $description; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="ficheProjet", type="string", length=255, nullable=true) 
    */ 
    private $ficheProjet; 

    /** 
    * @Assert\File(maxSize="6000000") 
    */ 
    public $file; 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set idMembre 
    * 
    * @param integer $idMembre 
    * @return Projet 
    */ 
    public function setIdMembre($idMembre) 
    { 
     $this->idMembre = $idMembre; 

     return $this; 
    } 

    /** 
    * Get idMembre 
    * 
    * @return integer 
    */ 
    public function getIdMembre() 
    { 
     return $this->idMembre; 
    } 

    /** 
    * Set idAssociation 
    * 
    * @param integer $idAssociation 
    * @return Projet 
    */ 
    public function setIdAssociation($idAssociation) 
    { 
     $this->idAssociation = $idAssociation; 

     return $this; 
    } 

    /** 
    * Get idAssociation 
    * 
    * @return integer 
    */ 
    public function getIdAssociation() 
    { 
     return $this->idAssociation; 
    } 


    /** 
    * Set StatutProjet 
    * 
    * @param integer $StatutProjet 
    * @return Tache 
    */ 
    public function setStatutProjet($StatutProjet) 
    { 
     $this->StatutProjet = $StatutProjet; 

     return $this; 
    } 

    /** 
    * Get StatutProjet 
    * 
    * @return integer 
    */ 
    public function getStatutProjet() 
    { 
     return $this->StatutProjet; 
    } 

    /** 
    * Set nomProjet 
    * 
    * @param string $nomProjet 
    * @return Projet 
    */ 
    public function setNomProjet($nomProjet) 
    { 
     $this->nomProjet = $nomProjet; 

     return $this; 
    } 

    /** 
    * Get nomProjet 
    * 
    * @return string 
    */ 
    public function getNomProjet() 
    { 
     return $this->nomProjet; 
    } 

    /** 
    * Set visibilite 
    * 
    * @param boolean $visibilite 
    * @return Projet 
    */ 
    public function setVisibilite($visibilite) 
    { 
     $this->visibilite = $visibilite; 

     return $this; 
    } 

    /** 
    * Get visibilite 
    * 
    * @return boolean 
    */ 
    public function getVisibilite() 
    { 
     return $this->visibilite; 
    } 

    /** 
    * Set dateDebut 
    * 
    * @param string $dateDebut 
    * @return Projet 
    */ 
    public function setDateDebut($dateDebut) 
    { 
     $this->dateDebut = $dateDebut; 

     return $this; 
    } 

    /** 
    * Get dateDebut 
    * 
    * @return string 
    */ 
    public function getDateDebut() 
    { 
     return $this->dateDebut; 
    } 

    /** 
    * Set dateFin 
    * 
    * @param string $dateFin 
    * @return Projet 
    */ 
    public function setDateFin($dateFin) 
    { 
     $this->dateFin = $dateFin; 

     return $this; 
    } 

    /** 
    * Get dateFin 
    * 
    * @return string 
    */ 
    public function getDateFin() 
    { 
     return $this->dateFin; 
    } 

    /** 
    * Set budget 
    * 
    * @param float $budget 
    * @return Projet 
    */ 
    public function setBudget($budget) 
    { 
     $this->budget = $budget; 

     return $this; 
    } 

    /** 
    * Get budget 
    * 
    * @return float 
    */ 
    public function getBudget() 
    { 
     return $this->budget; 
    } 

    /** 
    * Set budgetActuel 
    * 
    * @param float $budgetActuel 
    * @return Projet 
    */ 
    public function setBudgetActuel($budgetActuel) 
    { 
     $this->budgetActuel = $budgetActuel; 

     return $this; 
    } 

    /** 
    * Get budgetActuel 
    * 
    * @return float 
    */ 
    public function getBudgetActuel() 
    { 
     return $this->budgetActuel; 
    } 

    /** 
    * Set description 
    * 
    * @param string $description 
    * @return Projet 
    */ 
    public function setDescription($description) 
    { 
     $this->description = $description; 

     return $this; 
    } 

    /** 
    * Get description 
    * 
    * @return string 
    */ 
    public function getDescription() 
    { 
     return $this->description; 
    } 

    /** 
    * Set ficheProjet 
    * 
    * @param string $ficheProjet 
    * @return Projet 
    */ 
    public function setFicheProjet($ficheProjet) 
    { 
     $this->ficheProjet = $ficheProjet; 

     return $this; 
    } 

    /** 
    * Get ficheProjet 
    * 
    * @return string 
    */ 
    public function getFicheProjet() 
    { 
     return $this->ficheProjet; 
    } 


    public function getAbsolutePath() 
    { 
     return null === $this->ficheProjet ? null : $this->getUploadRootDir().'/'.$this->ficheProjet; 
    } 

    public function getWebPath() 
    { 
     return null === $this->ficheProjet ? null : $this->getUploadDir().'/'.$this->ficheProjet; 
    } 

    protected function getUploadRootDir() 
    { 
     // le chemin absolu du répertoire où les documents uploadés doivent être sauvegardés 
     return __DIR__.'/../../../../web/'.$this->getUploadDir(); 
    } 

    protected function getUploadDir() 
    { 
     // on se débarrasse de « __DIR__ » afin de ne pas avoir de problème lorsqu'on affiche 
     // le document/image dans la vue. 
     return 'uploads/documents'; 
    } 


/** 
    * @ORM\PrePersist() 
    * @ORM\PreUpdate() 
    */ 
    public function preUpload() 
    { 
     if (null !== $this->file) { 
      // faites ce que vous voulez pour générer un nom unique 
      $this->ficheProjet = sha1(uniqid(mt_rand(), true)).'.'.$this->file->guessExtension(); 
     } 
    } 

    /** 
    * @ORM\PostPersist() 
    * @ORM\PostUpdate() 
    */ 
    public function upload() 
    { 
     if (null === $this->file) { 
      return; 
     } 

     // s'il y a une erreur lors du déplacement du fichier, une exception 
     // va automatiquement être lancée par la méthode move(). Cela va empêcher 
     // proprement l'entité d'être persistée dans la base de données si 
     // erreur il y a 
     $this->file->move($this->getUploadRootDir(), $this->ficheProjet); 

     unset($this->file); 
    } 

    /** 
    * @ORM\PostRemove() 
    */ 
    public function removeUpload() 
    { 
     if ($file = $this->getAbsolutePath()) { 
      unlink($file); 
     } 
    } 



} 

NB: Я имею в виду ficheProjet по пути. Anny предложение? И спасибо.

ответ

2

Вы должны попытаться использовать Doctrine Uploadable Extension

Вы должны установить StofDoctrineExtensionBundle

После того, как вы должны быть в состоянии добавить в вашей организации:

namespace My\Bundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Gedmo\Mapping\Annotation as Gedmo; 
use Symfony\Component\Validator\Constraints as Assert; 

/** 
* MyEntity 
* 
* @ORM\Table(name="my_entity") 
* @ORM\Entity() 
* @Gedmo\Uploadable(
*  path="uploads/my_entity", 
*  allowOverwrite=true, 
*  allowedTypes="image/jpeg,image/pjpeg,image/png,image/x-png" 
*) 
*/ 
class MyEntity 
{ 
    //... 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="picture", type="string", length=255, nullable=true) 
    * @Gedmo\UploadableFilePath 
    * @Assert\File(
    *  mimeTypes={"image/jpeg", "image/pjpeg", "image/png", "image/x-png"} 
    *) 
    */ 
    private $picture; 

    //... 
} 

Не забудьте создать выгрузку папку и установить права доступа:

mkdir -p web/uploads/my_entity 
chmod -R 777 web/uploads 
+0

Извините, я f orgot, чтобы упомянуть, что я проверяю свою форму с помощью ajax (jquery). Когда я прочитал мой заголовок ответа, я обнаружил, что все поля существуют, кроме поля файла 'projet_classebundle_projettype [nomProjet]: БББ projet_classebundle_projettype [idMembre]: 35 projet_classebundle_projettype [dateDebut]: 22/22/2222 projet_classebundle_projettype [dateFin]: 22/22/2222 projet_classebundle_projettype [statutProjet]: 1 projet_classebundle_projettype [visibilite]: 1 projet_classebundle_projettype [бюджет]: 2c projet_classebundle_projettype [описание]: cc' – 2dar

Смежные вопросы