Menu

Application Symfony2 : Part 8 Upload des fichier avec doctrine

Introduction 

Nous allons voir dans ce Tuto comment gérer l’upload des fichiers avec doctrine pour notre entité FILM

Mise à jour de l'entité Film et la base de données

Nous allons rendre visite au site Symfony.com et dans la zone de recherche tapez "upload file doctrine"
https://symfony.com/doc/2.7/cookbook/doctrine/file_uploads.html
Maintenant copier la variable path et les autres fonction dans votre entité Film
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    public $path;

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

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

    protected function getUploadRootDir()
    {
        // the absolute directory path where uploaded
        // documents should be saved
        return __DIR__.'/../../../../web/'.$this->getUploadDir();
    }

    protected function getUploadDir()
    {
        // get rid of the __DIR__ so it doesn't screw up
        // when displaying uploaded doc/image in the view.
        return 'uploads/films';
    }
  1. La variable path c’est le chemin de notre fichier 
  2. Changer le chemin d’upload dans fonction getUploadDir() uploads/films
  3. Maintenant nous allons ajouter un champ de type file dans FilmType.php
     ->add('file')
  4. Ajouter cette attribut à notre Entité film n'oublié pas les getters et setters et  les Use pour le cas des Assert et file.

    use Symfony\Component\Validator\Constraints as Assert;
    use Symfony\Component\HttpFoundation\File\UploadedFile;
    
    

         /**
         * @Assert\File(maxSize="6000000")
         */
        private $file;
    
        /**
         * Sets file.
         *
         * @param UploadedFile $file
         */
        public function setFile(UploadedFile $file = null)
        {
            $this->file = $file;
        }
    
        /**
         * Get file.
         *
         * @return UploadedFile
         */
        public function getFile()
        {
            return $this->file;
        }
  5. Ajouter Fonction upload() dans Entité Film 
    public function upload()
    {
        if (null === $this->getFile()) {
            return;
        }
    
        $this->getFile()->move(
            $this->getUploadRootDir(),
            $this->getFile()->getClientOriginalName()
        );
    
        $this->path = $this->getFile()->getClientOriginalName();
    
        $this->file = null;
    }
  6. Dans Controller il faut faire un appel à la fonction upload dans la fonction createAction()
    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
    
        $entity->upload();
    
        $em->persist($entity);
        $em->flush();
    }
  7. Maintenant nous devons faire un mise à jour de la base de données
    php app/console doctrine:schema:update --force

Affichage dans View 

  1. <img src="{{ asset('uploads/films/'~entity.path) }}" >

    Le tilde " ~ " c’est pour faire la concatenation


Merci pour Votre Attention

1 commentaire:

  1. Thanks for sharing this informative article. I have been looking for all this information for learning tourism company web design and got it all here.

    RépondreSupprimer