Bonjour à tous,

Après lecture de la doc symfony sur l'upload de fichier et après recherche sur Gogole sans réussite, ma dernière carte est le forum et votre aide

Je veux tout simplement permettre à mes utilisateurs lors de leur inscription d'uploader une image de profil.

J'ai donc deux entités avec des relations bidirectionnelles USER (oneToMany) et IMAGE (manyToOne).

Voici ce que j'ai fais :

Mon entité USER :
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
<?php
// src/feel/UserBundle/Entity/User.php
 
namespace feel\UserBundle\Entity;
 
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
 
/**
 * @ORM\Entity
 * @ORM\Table(name="user")
 * @ORM\Entity(repositoryClass="UserRepository"))
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
 
    /**
     * @var string
     *
     * @ORM\Column(name="prenom", type="string", length=255, nullable=true)
     */
    private $prenom;
 
    /**
     * @var \DateTime
     *
     * @ORM\Column(name="dateNaissance", type="date")
     */
    private $dateNaissance;
 
    /**
     * @var boolean
     *
     * @ORM\Column(name="sexe", type="string", length=10)
     */
    private $sexe;
 
    /**
     * @var string
     *
     * @ORM\Column(name="diplome", type="string", length=50, nullable=true)
     */
    private $diplome;
 
    /**
     * @var string
     *
     * @ORM\Column(name="statutFamiliale", type="string", length=25, nullable=true)
     */
    private $statutFamiliale;
 
    /**
     * @var string
     *
     * @ORM\Column(name="profession", type="string", length=25, nullable=true)
     */
    private $profession;
 
    /**
     * @var string
     *
     * @ORM\Column(name="religion", type="string", length=25, nullable=true)
     */
    private $religion;
 
    /**
     * @var integer
     *
     * @ORM\Column(name="enfants", type="integer", length=2, nullable=true)
     */
    private $enfants;
 
    /**
     * @var integer
     *
     * @ORM\Column(name="codePostal", type="integer", length=10, nullable=true)
     */
    private $codePostal;
 
    /**
     * @var string
     *
     * @ORM\Column(name="films", type="string", length=255, nullable=true)
     */
    private $films;
 
    /**
     * @var string
     *
     * @ORM\Column(name="livres", type="string", length=255, nullable=true)
     */
    private $livres;
 
    /**
     * @var string
     *
     * @ORM\Column(name="artistes", type="string", length=255, nullable=true)
     */
    private $artistes;
 
    /**
     * @var string
     *
     * @ORM\Column(name="soirees", type="string", length=255, nullable=true)
     */
    private $soirees;
 
    /**
     * @var string
     *
     * @ORM\Column(name="voyages", type="string", length=255, nullable=true)
     */
    private $voyages;
 
    /**
     * @var string
     *
     * @ORM\Column(name="attentes", type="string", length=255, nullable=true)
     */
    private $attentes;
 
    /**
     * @var string
     *
     * @ORM\Column(name="qualites", type="string", length=255, nullable=true)
     */
    private $qualites;
 
    /**
     * @var string
     *
     * @ORM\Column(name="defauts", type="string", length=255, nullable=true)
     */
    private $defauts;
 
    /**
     * @var string
     *
     * @ORM\Column(name="description", type="string", length=255, nullable=true)
     */
    private $description;
 
    /**
     * @var string
     *
     * @ORM\Column(name="hobbies", type="string", length=255, nullable=true)
     */
    private $hobbies;
 
    /**
     * @var \DateTime
     *
     * @ORM\Column(name="lastActivity", type="datetime", nullable=true)
     */
    private $lastActivity;
 
    /**
    * @ORM\OneToMany(targetEntity="feel\UserBundle\Entity\Image", mappedBy="user")
    */
    private $image;
 
 
    public function __construct()
    {
        parent::__construct();
        // your own logic
        $this->enabled = true;
        $this->addRole('ROLE_INSCRIT');
    }
 
 
    /**
     * Set prenom
     *
     * @param string $prenom
     * @return User
     */
    public function setPrenom($prenom)
    {
        $this->prenom = $prenom;
 
        return $this;
    }
 
    /**
     * Get prenom
     *
     * @return string 
     */
    public function getPrenom()
    {
        return $this->prenom;
    }
 
    /**
     * Set lastActivity
     *
     * @param \DateTime $lastActivity
     * @return User
     */
    public function setLastActivity(\DateTime $lastActivity)
    {
        $this->lastActivity = $lastActivity;
 
        return $this;
    }
 
    /**
     * Get lastActivity
     *
     * @return \DateTime 
     */
    public function getLastActivity()
    {
        return $this->lastActivity;
    }
 
    public function isActiveNow()
    {
        $this->setLastActivity(new \DateTime());
    }
 
 
    /**
     * Set dateNaissance
     *
     * @param \DateTime $dateNaissance
     * @return User
     */
    public function setDateNaissance($dateNaissance)
    {
        $this->dateNaissance = $dateNaissance;
 
        return $this;
    }
 
    /**
     * Get dateNaissance
     *
     * @return \DateTime 
     */
    public function getDateNaissance()
    {
        return $this->dateNaissance;
    }
 
    /**
     * Set sexe
     *
     * @param string $sexe
     * @return User
     */
    public function setSexe($sexe)
    {
        $this->sexe = $sexe;
 
        return $this;
    }
 
    /**
     * Get sexe
     *
     * @return string 
     */
    public function getSexe()
    {
        return $this->sexe;
    }
 
    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
 
    /**
     * Set diplome
     *
     * @param string $diplome
     * @return User
     */
    public function setDiplome($diplome)
    {
        $this->diplome = $diplome;
 
        return $this;
    }
 
    /**
     * Get diplome
     *
     * @return string 
     */
    public function getDiplome()
    {
        return $this->diplome;
    }
 
    /**
     * Set statutFamiliale
     *
     * @param string $statutFamiliale
     * @return User
     */
    public function setStatutFamiliale($statutFamiliale)
    {
        $this->statutFamiliale = $statutFamiliale;
 
        return $this;
    }
 
    /**
     * Get statutFamiliale
     *
     * @return string 
     */
    public function getStatutFamiliale()
    {
        return $this->statutFamiliale;
    }
 
    /**
     * Set profession
     *
     * @param string $profession
     * @return User
     */
    public function setProfession($profession)
    {
        $this->profession = $profession;
 
        return $this;
    }
 
    /**
     * Get profession
     *
     * @return string 
     */
    public function getProfession()
    {
        return $this->profession;
    }
 
    /**
     * Set religion
     *
     * @param string $religion
     * @return User
     */
    public function setReligion($religion)
    {
        $this->religion = $religion;
 
        return $this;
    }
 
    /**
     * Get religion
     *
     * @return string 
     */
    public function getReligion()
    {
        return $this->religion;
    }
 
    /**
     * Set enfants
     *
     * @param integer $enfants
     * @return User
     */
    public function setEnfants($enfants)
    {
        $this->enfants = $enfants;
 
        return $this;
    }
 
    /**
     * Get enfants
     *
     * @return integer 
     */
    public function getEnfants()
    {
        return $this->enfants;
    }
 
    /**
     * Set codePostal
     *
     * @param integer $codePostal
     * @return User
     */
    public function setCodePostal($codePostal)
    {
        $this->codePostal = $codePostal;
 
        return $this;
    }
 
    /**
     * Get codePostal
     *
     * @return integer 
     */
    public function getCodePostal()
    {
        return $this->codePostal;
    }
 
    /**
     * Set films
     *
     * @param string $films
     * @return User
     */
    public function setFilms($films)
    {
        $this->films = $films;
 
        return $this;
    }
 
    /**
     * Get films
     *
     * @return string 
     */
    public function getFilms()
    {
        return $this->films;
    }
 
    /**
     * Set livres
     *
     * @param string $livres
     * @return User
     */
    public function setLivres($livres)
    {
        $this->livres = $livres;
 
        return $this;
    }
 
    /**
     * Get livres
     *
     * @return string 
     */
    public function getLivres()
    {
        return $this->livres;
    }
 
    /**
     * Set artistes
     *
     * @param string $artistes
     * @return User
     */
    public function setArtistes($artistes)
    {
        $this->artistes = $artistes;
 
        return $this;
    }
 
    /**
     * Get artistes
     *
     * @return string 
     */
    public function getArtistes()
    {
        return $this->artistes;
    }
 
    /**
     * Set soirees
     *
     * @param string $soirees
     * @return User
     */
    public function setSoirees($soirees)
    {
        $this->soirees = $soirees;
 
        return $this;
    }
 
    /**
     * Get soirees
     *
     * @return string 
     */
    public function getSoirees()
    {
        return $this->soirees;
    }
 
    /**
     * Set voyages
     *
     * @param string $voyages
     * @return User
     */
    public function setVoyages($voyages)
    {
        $this->voyages = $voyages;
 
        return $this;
    }
 
    /**
     * Get voyages
     *
     * @return string 
     */
    public function getVoyages()
    {
        return $this->voyages;
    }
 
    /**
     * Set attentes
     *
     * @param string $attentes
     * @return User
     */
    public function setAttentes($attentes)
    {
        $this->attentes = $attentes;
 
        return $this;
    }
 
    /**
     * Get attentes
     *
     * @return string 
     */
    public function getAttentes()
    {
        return $this->attentes;
    }
 
    /**
     * Set qualites
     *
     * @param string $qualites
     * @return User
     */
    public function setQualites($qualites)
    {
        $this->qualites = $qualites;
 
        return $this;
    }
 
    /**
     * Get qualites
     *
     * @return string 
     */
    public function getQualites()
    {
        return $this->qualites;
    }
 
    /**
     * Set defauts
     *
     * @param string $defauts
     * @return User
     */
    public function setDefauts($defauts)
    {
        $this->defauts = $defauts;
 
        return $this;
    }
 
    /**
     * Get defauts
     *
     * @return string 
     */
    public function getDefauts()
    {
        return $this->defauts;
    }
 
    /**
     * Set description
     *
     * @param string $description
     * @return User
     */
    public function setDescription($description)
    {
        $this->description = $description;
 
        return $this;
    }
 
    /**
     * Get description
     *
     * @return string 
     */
    public function getDescription()
    {
        return $this->description;
    }
 
    /**
     * Set hobbies
     *
     * @param string $hobbies
     * @return User
     */
    public function setHobbies($hobbies)
    {
        $this->hobbies = $hobbies;
 
        return $this;
    }
 
    /**
     * Get hobbies
     *
     * @return string 
     */
    public function getHobbies()
    {
        return $this->hobbies;
    }
 
    /**
     * Add image
     *
     * @param \feel\UserBundle\Entity\Image $image
     * @return User
     */
    public function addImage(\feel\UserBundle\Entity\Image $image)
    {
        $this->image[] = $image;
 
        return $this;
    }
 
    /**
     * Remove image
     *
     * @param \feel\UserBundle\Entity\Image $image
     */
    public function removeImage(\feel\UserBundle\Entity\Image $image)
    {
        $this->image->removeElement($image);
    }
 
    /**
     * Get image
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getImage()
    {
        return $this->image;
    }
}<br><br>
Mon entité IMAGE :
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
<?php
 
namespace feel\UserBundle\Entity;
 
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
 
/**
 * Image
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="feel\UserBundle\Entity\ImageRepository")
 */
class Image
{
 
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    public $id;
 
    /**
     * @ORM\Column(type="string", length=255)
     * @Assert\NotBlank
     */
    public $name;
 
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    public $path;
 
 
    /**
     * @Assert\File(maxSize="6000000")
     */
    public $file;
 
 
    /**
    * @ORM\ManyToOne(targetEntity="feel\UserBundle\Entity\User", inversedBy="image")
    * @ORM\JoinColumn(nullable=false)
    */
    private $user;
 
 
    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()
    {
        // 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/images-profil';
    }
 
 
     /**
     * Set user
     *
     * @param \feel\UserBundle\Entity\User $user
     * @return Image
     */
    public function setUser(\feel\UserBundle\Entity\User $user)
    {
        $this->user = $user;
 
        return $this;
    }
 
    /**
     * Get user
     *
     * @return \feel\UserBundle\Entity\User 
     */
    public function getUser()
    {
        return $this->user;
    }
 
 
 
    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
 
    /**
     * Set name
     *
     * @param string $name
     * @return Image
     */
    public function setName($name)
    {
        $this->name = $name;
 
        return $this;
    }
 
    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }
 
    /**
     * Set path
     *
     * @param string $path
     * @return Image
     */
    public function setPath($path)
    {
        $this->path = $path;
 
        return $this;
    }
 
    /**
     * Get path
     *
     * @return string 
     */
    public function getPath()
    {
        return $this->path;
    }
 
    public function upload()
{
    // la propriété « file » peut être vide si le champ n'est pas requis
    if (null === $this->file) {
        return;
    }
 
    // utilisez le nom de fichier original ici mais
    // vous devriez « l'assainir » pour au moins éviter
    // quelconques problèmes de sécurité
 
    // la méthode « move » prend comme arguments le répertoire cible et
    // le nom de fichier cible où le fichier doit être déplacé
    $this->file->move($this->getUploadRootDir(), $this->file->getClientOriginalName());
 
    // définit la propriété « path » comme étant le nom de fichier où vous
    // avez stocké le fichier
    $this->path = $this->file->getClientOriginalName();
 
    // « nettoie » la propriété « file » comme vous n'en aurez plus besoin
    $this->file = null;
}
 
 
 
 
}
Mon IMAGETYPE :
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
<?php
 
namespace feel\UserBundle\Form;
 
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
 
class ImageType extends AbstractType
{
        /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('file')
        ;
    }
 
    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'feel\UserBundle\Entity\Image'
        ));
    }
 
    /**
     * @return string
     */
    public function getName()
    {
        return 'feel_userbundle_image';
    }
}
Et mon action de validation du formulaire dans mon controlleur :
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
public function suiteInscriptionAction(Request $request){
 
        $user = $this->container->get('security.context')->getToken()->getUser();
 
        /*if($user->getDescription() != "") {
            return $this->redirect($this->generateUrl("homepage"));
        }*/
 
        $formBuilder = $this->get('form.factory')->createBuilder('form', $user);
 
        $formBuilder
            ->add('image', new ImageType(),array('mapped'=>false))
            ->add('username',      'text', array('required' => true))
            ->add('diplome',      'text', array('required' => false))
            ->add('statutFamiliale','choice', array(
                   'choices' => array('' => '- Sélectionner -', 'celibataire' => 'Célibataire', 'divorce' => 'Divorcé'),
                   'required' => false
               ))
            ->add('profession',      'text', array('required' => false))
            ->add('religion',      'text', array('required' => false))
            ->add('enfants',      'text', array('required' => false))
            ->add('codePostal',      'text')
            ->add('films',      'text', array('required' => false))
            ->add('livres',      'text', array('required' => false))
            ->add('artistes',      'text', array('required' => false))
            ->add('soirees',      'text', array('required' => false))
            ->add('voyages',      'text', array('required' => false))
            ->add('attentes',      'text', array('required' => false))
            ->add('qualites',      'text')
            ->add('defauts',      'text')
            ->add('description',      'textarea')
            ->add('hobbies',      'text')
            ->getForm()
          ;
 
        $form = $formBuilder->getForm();
 
        $form->handleRequest($request);
 
        if($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
 
            $image = new Image();
            $image->upload();
 
            $em->persist($user);
            $em->flush();
 
            $request->getSession()->getFlashBag()->add('notice','Données enregistrées');
 
            return $this->redirect($this->generateUrl("homepage"));
        }
 
        return $this->render("feelUserBundle:Registration:suiteInscription.html.twig", array(
           'form' => $form->createView(),
       ));
    }
Lors de la soumission je n'ai aucune image persistée en BDD et je n'ai pas d'image nan plus dans mon dossier uploads/images-profil. Par contre les champs de l'entité User sont bien persistés eux.

Merci pour votre aide car je compte beaucoup dessus n'ayant pas trouvé de solutions.