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