IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Symfony PHP Discussion :

[Form/Upload] Erreur lors de deux upload différents dans un form


Sujet :

Symfony PHP

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Développeur informatique
    Inscrit en
    Avril 2010
    Messages
    17
    Détails du profil
    Informations personnelles :
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2010
    Messages : 17
    Par défaut [Form/Upload] Erreur lors de deux upload différents dans un form
    Bonjour,

    Le problème qui se pose à moi aujourd'hui concerne mon entité "Gamme.php".

    Cette gamme possède deux propriétés : "documentation" et "picto" qui nécessite l'utilisation d'upload dans le formulaire de création / modification.

    Gamme.php
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
     
    <?php
     
    namespace Twn\VehiculeBundle\Entity;
     
    use Symfony\Component\HttpFoundation\File\UploadedFile;
     
    use Doctrine\ORM\Mapping as ORM;
    use Doctrine\ORM\PersistentCollection as PersistentCollection;
    use Symfony\Component\Validator\Constraints as Assert;
     
    /**
     * Twn\VehiculeBundle\Entity\Gamme
     *
     * @ORM\Table()
     * @ORM\Entity(repositoryClass="Twn\VehiculeBundle\Entity\GammeRepository")
     * @ORM\HasLifecycleCallbacks()
     */
    class Gamme
    {
        /**
         * @var integer $id
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
     
        /**
         * @var text $presentation
         *
         * @ORM\Column(name="presentation", type="text")
         */
        private $presentation;
     
        /**
         * @var string $libelle
         *
         * @ORM\Column(name="libelle", type="string")
         */
        private $libelle;
     
        /**
         * @var string $picto
         *
         * @ORM\Column(name="picto", type="string", nullable=true)
         */
        private $picto;
     
        /**
         * @var string $documentation
         *
         * @ORM\Column(name="documentation", type="string", nullable=true)
         */
        private $documentation;
     
       /**
         * @var integer $orderBy
         *
         * @ORM\Column(name="order_by", type="integer", nullable=true)
         */
        private $orderBy;
     
     
        /**
         * @ORM\OneToMany(targetEntity="Vehicule", mappedBy="gamme", cascade={"persist"})
         */
        protected $vehicules;
     
        /**
         * @ORM\ManyToOne(targetEntity="Marque", inversedBy="gammes")
         * @ORM\JoinColumn(name="marque_id", referencedColumnName="id")
         */
        protected $marque;
     
        /**
         * @ORM\ManyToOne(targetEntity="GammeType", inversedBy="gammes")
         * @ORM\JoinColumn(name="typegamme_id", referencedColumnName="id")
         */
        protected $typeGamme;
     
        /**
         * @ORM\OneToMany(targetEntity="GammeImage", mappedBy="gamme", cascade={"remove", "persist"})
         */
        protected $images;
     
     
        /**
         * @Assert\File(maxSize="2000000")
         */
        public $file_p;
     
        /**
         * @Assert\File(maxSize="7000000")
         */
        public $file_d;
     
     
        public function __toString(){
        	return $this->getLibelle();
        }
     
        public function __construct()
        {
            $this->vehicules = new \Doctrine\Common\Collections\ArrayCollection();
        	$this->images = new \Doctrine\Common\Collections\ArrayCollection();
        }
     
    /***** Je vous épargne les getters / setters *****/
     
        public function getFileD(){
        	return $this->file_d;
        }
     
    	public function getFileP(){
        	return $this->file_p;
        }
     
    	public function getAbsolutePathPicto()
        {
        	return null === $this->getPicto() ? null : $this->getUploadRootDirPicto().'/'.$this->getPicto();
        }
     
        public function getWebPathPicto()
        {
        	return null === $this->getPicto() ? null : '/'.$this->getUploadDirPicto().'/'.$this->getPicto();
        }
     
        protected function getUploadRootDirPicto()
        {
        	// the absolute directory path where uploaded documents should be saved
        	return __DIR__.'/../../../../web/'.$this->getUploadDirPicto();
        }
     
        protected function getUploadDirPicto()
        {
        	// get rid of the __DIR__ so it doesn't screw when displaying uploaded doc/image in the view.
        	return 'uploads/gamme/picto';
        }
     
     
        public function getAbsolutePathDoc()
        {
        	return null === $this->getDocumentation() ? null : $this->getUploadRootDirDoc().'/'.$this->getDocumentation();
        }
     
        public function getWebPathDoc()
        {
        	return null === $this->getDocumentation() ? null : '/'.$this->getUploadDirDoc().'/'.$this->getDocumentation();
        }
     
        protected function getUploadRootDirDoc()
        {
        	// the absolute directory path where uploaded documents should be saved
        	return __DIR__.'/../../../../web/'.$this->getUploadDirDoc();
        }
     
        protected function getUploadDirDoc()
        {
        	// get rid of the __DIR__ so it doesn't screw when displaying uploaded doc/image in the view.
        	return 'uploads/gamme/doc';
        }
     
        /**
         * @ORM\PrePersist()
         * @ORM\PreUpdate()
         */
        public function preUploadPic()
        {
        	if (null !== $this->file_p) {
        		// do whatever you want to generate a unique name
        		$this->setPicto(uniqid().'.'.$this->file_p->guessExtension());
        	}
        }
     
        /**
         * @ORM\PrePersist()
         * @ORM\PreUpdate()
         */
        public function preUploadDoc()
        {
        	if (null !== $this->getFileD()) {
        		// do whatever you want to generate a unique name
        		$this->setDocumentation(uniqid().'.'.$this->file_d->guessExtension());
        	}
        }
     
        /**
         * @ORM\PostPersist()
         * @ORM\PostUpdate()
         */
        public function uploadPicto()
        {
        	if (null === $this->file_p) {
        		return;
        	}
     
        	// if there is an error when moving the file, an exception will
        	// be automatically thrown by move(). This will properly prevent
        	// the entity from being persisted to the database on error
        	$this->file_p->move($this->getUploadRootDirPicto(), $this->getPicto());  
     
        	unset($this->file_p);
        }
     
        /**
         * @ORM\PostPersist()
         * @ORM\PostUpdate()
         */
        public function uploadDoc()
        {
        	if (null === $this->file_d) {
        		return;
        	}
     
        	// if there is an error when moving the file, an exception will
        	// be automatically thrown by move(). This will properly prevent
        	// the entity from being persisted to the database on error
        	$this->file_d->move($this->getUploadRootDirDoc(), $this->getDocumentation());
     
        	unset($this->file_d);
        }
     
        /**
         * @ORM\PostRemove()
         */
        public function removeUploadPicto()
        {
        	if ($file_p = $this->getAbsolutePathPicto()) {
        		if (file_exists($file_p)) unlink($file_p);
        	}
        }
     
        /**
         * @ORM\PostRemove()
         */
        public function removeUploadDoc()
        {
        	if ($file_d = $this->getAbsolutePathDoc()) {
        		if (file_exists($file_d)) unlink($file_d);
        	}
        }
     
     
    }
    GammeType.php
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
     
    <?php 
    namespace Twn\VehiculeBundle\Form\Type;
     
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilder;
     
    use Twn\VehiculeBundle\Entity\MarqueRepository;
    use Twn\VehiculeBundle\Entity\GammeTypeRepository;
    use Twn\VehiculeBundle\Form\Type\GammeImageType;
     
    class GammeType extends AbstractType
    {
    	private $estPro;
     
    	public function __construct($estPro = false){
    		$this->estPro = $estPro;
    	}
     
        public function buildForm(FormBuilder $builder, array $options)
        {
            $builder->add('presentation', 'textarea', array('label' => 'Présentation', 'attr' => array('class' => 'tinymce', 'data-theme' => 'medium')));
            $builder->add('libelle', 'text', array('label' => 'Libellé'));
            $builder->add('file_p', 'file', array('label' => 'Pictogramme', 'required' => false));
            $builder->add('file_d', 'file', array('label' => 'Documentation ', 'required' => false));
            $builder->add('orderBy', 'hidden', array('label' => 'Ordre d\'affichage', 'required' => false));
            $builder->add('marque', 'entity', array('class' => 'TwnVehiculeBundle:Marque',  'empty_value' => '-- Sélectionnez --', 'label' => 'Marque', 'property' => 'libBase',
                            'query_builder' => function(MarqueRepository $m) {
                                    return $m->createQueryBuilder('m')
                                            ->where('m.venteNeuf = 1')
                                            ->orderBy('m.libelle', 'ASC');}));
     
            if($this->estPro){                               
    	        $builder->add('typeGamme', 'entity', array('class' => 'TwnVehiculeBundle:GammeType',  'empty_value' => '-- Sélectionnez --', 'label' => 'Type de gamme',
    	                        'query_builder' => function(GammeTypeRepository $gt) {
    	                                return $gt->createQueryBuilder('gt')
    	                                        ->where('gt.estPro = 1')
    	                                        ->orderBy('gt.libelle', 'ASC');}));
            }else{
               $builder->add('typeGamme', 'entity', array('class' => 'TwnVehiculeBundle:GammeType',  'empty_value' => '-- Sélectionnez --', 'label' => 'Type de gamme',
                                'query_builder' => function(GammeTypeRepository $gt) {
                                        return $gt->createQueryBuilder('gt')
                                                ->where('gt.estPro = 0')
                                                ->orderBy('gt.libelle', 'ASC');}));
            }
            $builder->add('images', 'collection', array('type' => new GammeImageType(), 'allow_add' => true,
            'by_reference' => false,'allow_delete' => true,'required' => false, 'label' => 'Images'));
        }
     
        public function getName()
        {
            return 'gamme';
        }
     
        public function getDefaultOptions(array $options)
        {
            return array(
                'data_class' => 'Twn\VehiculeBundle\Entity\Gamme',
            );
        }
    }
    J'utilise également un GammeFormHandler basique...


    Le problème :

    Lors d'un upload d'un, ou des deux fichiers (facultatifs tout deux), une erreur survient concernant tantôt $file_p, tantôt $file_d...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Notice: Undefined property: Twn\VehiculeBundle\Entity\Gamme::$file_p in C:\xampp\htdocs\3wnet-gibaud\src\Twn\VehiculeBundle\Entity\Gamme.php line 377
    On dirait qu'il n'apprécie pas le fait d'avoir deux uploads dans un même formulaire...

    En vous remerciant par avance de votre aide.

    Leya

  2. #2
    Expert confirmé

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920
    Par défaut
    y'a pas de ligne 377

  3. #3
    Membre averti
    Profil pro
    Développeur informatique
    Inscrit en
    Avril 2010
    Messages
    17
    Détails du profil
    Informations personnelles :
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2010
    Messages : 17
    Par défaut
    J'ai voulu épargner les getter et les setters , l'erreur se situe ici :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    /**
         * @ORM\PrePersist()
         * @ORM\PreUpdate()
         */
        public function preUploadPic()
        {
        	if (null !== $this->file_p) {
        		// do whatever you want to generate a unique name
        		$this->setPicto(uniqid().'.'.$this->file_p->guessExtension());
        	}
        }

  4. #4
    Expert confirmé

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920
    Par défaut
    t'as bien vidé ton cache ?

  5. #5
    Membre averti
    Profil pro
    Développeur informatique
    Inscrit en
    Avril 2010
    Messages
    17
    Détails du profil
    Informations personnelles :
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2010
    Messages : 17
    Par défaut
    Oui, via commande ou a la main... (même si ce n'est pas correct)

Discussions similaires

  1. Erreur lors de l'upload d'un fichier par formulaire
    Par loukoum82 dans le forum Tomcat et TomEE
    Réponses: 2
    Dernier message: 06/10/2008, 09h45
  2. [Upload] Erreur lors du déplacement de l'image
    Par javaboy dans le forum Langage
    Réponses: 4
    Dernier message: 16/10/2007, 14h56
  3. [upload] erreur lors d un upload
    Par venomelektro dans le forum Langage
    Réponses: 1
    Dernier message: 07/04/2007, 11h46
  4. Réponses: 4
    Dernier message: 08/11/2006, 18h28

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo