Bonjour, je débute dans symfony, et après avoir beaucoup cherché je rencontre un problème au niveau du multiupload.

Afin de pouvoir le gérer j'ai utilisé les 2 fichiers suivants que j'ai trouvé sur Github : https://gist.github.com/tristanlins/8784568

Cela fonctionne car je peux sélectionner maintenant plusieurs fichiers, le problème c'est que du coup j'obtiens l'erreur suivante :

Expected argument of type "string", "array" given
Je souhaite au moment ou j'upload mes photos récupéré aussi les données pour les persistées, je pense que le problème se situe à cette étape.

Mon controller :

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
 
<?php
 
namespace Mariage\BoBundle\Controller;
 
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 
use Mariage\BoBundle\Entity\Photos;
use Mariage\BoBundle\Form\PhotosType;
 
/**
 * Blog controller.
 *
 */
class PhotosController extends Controller
{
 
    /**
     * Lists all Blog entities.
     *
     */
    public function indexAction()
    {
 
 
        $em = $this->getDoctrine()->getManager();
 
        $entities = $em->getRepository('MariageBoBundle:Photos')->findAll();
 
 
        return $this->render('MariageBoBundle:Photos:index.html.twig', array(
            'entities' => $entities,
        ));
    }
 
    /**
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function newAction(Request $request)
    {
        $photos = new Photos();
 
        $form = $this->createForm(new PhotosType(), $photos, array(
            'action' => $this->generateUrl('photos_new'),
        ));
 
        $form->add('Ajouter', 'submit', array("attr" =>array('class' => 'btn btn-primary','label' => 'Create')));
 
        $form->handleRequest($request);
 
        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $photos->setUsers($this->getUser());
            $photos->setDate(new \Datetime());
            $em->persist($photos);
            $em->flush();
            return $this->redirect($this->generateUrl('photos'));
        }
 
 
        return $this->render('MariageBoBundle:Photos:new.html.twig',
            array(
                'form' => $form -> createView())
        );
 
    }
 
}
Mon form :

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
 
<?php
 
namespace Mariage\BoBundle\Form;
 
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
 
class PhotosType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('nom',null,array("label" => "Nom", "attr" =>array("placeholder" => "Votre Nom","class"=>"form-control")))
            ->add('prenom',null,array("label" => "Prénom", "attr" =>array("placeholder" => "Votre Prénom","class"=>"form-control")))
            ->add('file',new FilesType(),array("label" => "Photo"))
            ->add('categories','entity',array('class' => 'MariageBoBundle:Categories','property' => 'type',"label"=>"Catégorie","attr"=>array("class"=>"form-control")))
        ;
    }
 
    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Mariage\BoBundle\Entity\Photos'
        ));
    }
 
    /**
     * @return string
     */
    public function getName()
    {
        return 'mariage_bobundle_photos';
    }
}
Mon entité :

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
<?php
 
namespace Mariage\BoBundle\Entity;
 
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
 
/**
 * Photos
 *
 * @ORM\Table(name="photos", indexes={@ORM\Index(name="fk_photos_users1_idx", columns={"users_id"}), @ORM\Index(name="categories_id", columns={"categories_id"})})
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 */
class Photos
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;
 
    /**
     * @var string
     *
     * @ORM\Column(name="nom", type="string", length=255, nullable=true)
     * @Assert\NotBlank(message = "Précisez un nom !")
     * @Assert\Length(
     *      min = "2",
     *      minMessage = "Votre nom doit comporter au minimum 2 caractères"
     * )
     */
    private $nom;
 
    /**
     * @var string
     *
     * @ORM\Column(name="prenom", type="string", length=255, nullable=true)
     * @Assert\NotBlank(message = "Précisez un prénom !")
     * @Assert\Length(
     *      min = "2",
     *      minMessage = "Votre prénom doit comporter au minimum 2 caractères"
     * )
     */
    private $prenom;
 
    /**
     * @var string
     *
     * @ORM\Column(name="photo", type="text", nullable=true)
     */
    private $photo;
 
    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date", type="datetime", nullable=true)
     */
    private $date;
 
    /**
     * @var integer
     *
     * @ORM\Column(name="validation", type="integer", nullable=true)
     */
    private $validation = '0';
 
    /**
     * @var \Users
     *
     * @ORM\ManyToOne(targetEntity="Users")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="users_id", referencedColumnName="id")
     * })
     */
    private $users;
 
    /**
     * @var \Categories
     *
     * @ORM\ManyToOne(targetEntity="Categories")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="categories_id", referencedColumnName="id")
     * })
     */
    private $categories;
 
    /**
     */
    public $file;
 
    /**
     * @var
     */
    // propriété utilisé temporairement pour la suppression
    private $filenameForRemove;
 
 
    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
 
    /**
     * Set nom
     *
     * @param string $nom
     * @return Photos
     */
    public function setNom($nom)
    {
        $this->nom = $nom;
 
        return $this;
    }
 
    /**
     * Get nom
     *
     * @return string 
     */
    public function getNom()
    {
        return $this->nom;
    }
 
    /**
     * Set prenom
     *
     * @param string $prenom
     * @return Photos
     */
    public function setPrenom($prenom)
    {
        $this->prenom = $prenom;
 
        return $this;
    }
 
    /**
     * Get prenom
     *
     * @return string 
     */
    public function getPrenom()
    {
        return $this->prenom;
    }
 
    /**
     * Set photo
     *
     * @param string $photo
     * @return Photos
     */
    public function setPhoto($photo)
    {
        $this->photo = $photo;
 
        return $this;
    }
 
    /**
     * Get photo
     *
     * @return string 
     */
    public function getPhoto()
    {
        return $this->photo;
    }
 
    /**
     * Set date
     *
     * @param \DateTime $date
     * @return Photos
     */
    public function setDate($date)
    {
        $this->date = $date;
 
        return $this;
    }
 
    /**
     * Get date
     *
     * @return \DateTime 
     */
    public function getDate()
    {
        return $this->date;
    }
 
    /**
     * Set validation
     *
     * @param integer $validation
     * @return Photos
     */
    public function setValidation($validation)
    {
        $this->validation = $validation;
 
        return $this;
    }
 
    /**
     * Get validation
     *
     * @return integer 
     */
    public function getValidation()
    {
        return $this->validation;
    }
 
    /**
     * Set users
     *
     * @param \Mariage\BoBundle\Entity\Users $users
     * @return Photos
     */
    public function setUsers(\Mariage\BoBundle\Entity\Users $users = null)
    {
        $this->users = $users;
 
        return $this;
    }
 
    /**
     * Get users
     *
     * @return \Mariage\BoBundle\Entity\Users 
     */
    public function getUsers()
    {
        return $this->users;
    }
 
    /**
     * Set categories
     *
     * @param \Mariage\BoBundle\Entity\Categories $categories
     * @return Photos
     */
    public function setCategories(\Mariage\BoBundle\Entity\Categories $categories = null)
    {
        $this->categories = $categories;
 
        return $this;
    }
 
    /**
     * Get categories
     *
     * @return \Mariage\BoBundle\Entity\Categories 
     */
    public function getCategories()
    {
        return $this->categories;
    }
 
    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/photos';
    }
 
    /**
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function preUpload()
    {
        if (null !== $this->file) {
            // faites ce que vous voulez pour générer un nom unique
            $this->photo = 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->photo);
 
        unset($this->file);
    }
 
    /**
     * @ORM\PostRemove()
     */
    public function removeUpload()
    {
        if ($file = $this->getAbsolutePath()) {
            unlink($file);
        }
    }
}
Merci d'avance pour votre aide !