使用Doctrine上传文件 [英] File Uploads with Doctrine

查看:64
本文介绍了使用Doctrine上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Symfony2中使用Doctrine上传文件. 我按照本教程中的所有步骤操作教程,但是当我提交表格时我没有收到任何错误,但没有上传文件,并且表"document"中的路径为空.

这是我的实体

<?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按路径. 安妮的建议?谢谢.

解决方案

您应尝试使用文档可上传扩展

您必须安装 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

I 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

<?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 : I mean ficheProjet by path. Anny suggestion ? And thanks.

解决方案

You should try to use Doctrine Uploadable Extension

You have to install the StofDoctrineExtensionBundle

After 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_entity
chmod -R 777 web/uploads

这篇关于使用Doctrine上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆