在您应该可以添加您的实体之后: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_entitychmod -R 777 web/uploadsI am trying to upload files with Doctrine in Symfony2.I followed all the steps in this tutorial tutorial but whene I submit my form I get no errors but the file is not uploaded and the path in the table "document" is null.This is my entity<?phpnamespace 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 : I mean ficheProjet by path.Anny suggestion ? And thanks. 解决方案 You should try to use Doctrine Uploadable ExtensionYou have to install the StofDoctrineExtensionBundleAfter you should be able to add on your entity : 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; //...}Don't forget to create the upload folder and set right permissions:mkdir -p web/uploads/my_entitychmod -R 777 web/uploads 这篇关于使用Doctrine上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-03 18:52
查看更多