Bonsoir, je voudrais faire un formulaire imbriqué dans un autre formulaire sur Symfony. Donc j'ai la forme principale qui permet la création d'un "article" et le formulaire imbriqué qui est celui du téléchargement d'image. Cependant je rencontre une erreur qui est: "Expected argument of type "Nioro\NioroBundle\Entity\MediaUpload", "array" given.
Je comprend l'erreur mais j'arrive pas à le résoudre, j'ai besoin d'aide svp.
Je suis en version (3.1. *).
Je dépose mon code si ça peut aider.

MediaUpload.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
 
class MediaUpload 
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;
 
/**
 * @var \DateTime
 *
 * @ORM\Column(name="updateAt", type="datetimetz", nullable=true)
 */
private $updateAt;
 
/**
 * @ORM\PostLoad()
 */
public function postLoad()
{
    $this->updateAt = new \DateTime();
}
 
/**
 * @ORM\Column(type="string",length=255)
 * @Assert\NotBlank
 */
public $name;
 
/**
 * @ORM\Column(type="string",length=255, nullable=true)
 */
public $path;
 
public $file;
 
public function getUploadRootDir()
{
    return __dir__.'/../../../../web/images/uploads/article';
}
 
public function getAbsolutePath()
{
    return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path;
}
 
public function getAssetPath()
{
    return 'uploads/'.$this->path;
}
 
/**
 * @ORM\Prepersist()
 * @ORM\Preupdate()
 */
public function preUpload()
{
    $this->tempFile = $this->getAbsolutePath();
    $this->oldFile = $this->getPath();
    $this->updateAt = new \DateTime();
 
    if (null !== $this->file)
        $this->path = sha1(uniqid(mt_rand(),true)).'.'.$this->file->guessExtension();
}
 
/**
 * @ORM\PostPersist()
 * @ORM\PostUpdate()
 */
public function upload()
{
    if (null !== $this->file) {
        $this->file->move($this->getUploadRootDir(),$this->path);
        unset($this->file);
        if ($this->oldFile != null) unlink($this->tempFile);
    }
}
 
/**
 * @ORM\PreRemove()
 */
public function preRemoveUpload()
{
    $this->tempFile = $this->getAbsolutePath();
}
 
/**
 * @ORM\PostRemove()
 */
public function removeUpload()
{
    if (file_exists($this->tempFile)) unlink($this->tempFile);
}
 
/**
 * Get id
 *
 * @return int
 */
public function getId()
{
    return $this->id;
}
 
/**
 * @return mixed
 */
public function getName()
{
    var_dump($this->name);
    return $this->name;
}
 
/**
 * @return mixed
 */
public function getPath()
{
    return $this->path;
}
 
/**
 * Get updateAt
 *
 * @return \DateTime
 */
public function getUpdateAt()
{
    return $this->updateAt;
}
}
ArticleBlog.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
247
248
249
250
251
252
253
254
255
256
257
 
class ArticleBlog
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;
 
/**
 * @var string
 *
 * @ORM\Column(name="tittre", type="string", length=255)
 */
private $tittre;
 
/**
 * @var string
 *
 * @ORM\Column(name="auteur", type="string", length=100)
 */
private $auteur;
 
/**
 * @var string
 *
 * @ORM\Column(name="blog", type="text")
 */
private $blog;
 
/**
 * @ORM\OneToOne(targetEntity="Nioro\NioroBundle\Entity\MediaUpload", cascade={"persist","remove"})
 * @ORM\JoinColumn(nullable=false)
 */
private $image;
 
/**
 * @var \DateTime
 *
 * @ORM\Column(name="created", type="datetime")
 */
private $created;
 
/**
 * @var \DateTime
 *
 * @ORM\Column(name="updated", type="datetime")
 */
private $updated;
 
/**
 * @ORM\ManyToOne(targetEntity="Nioro\NioroBundle\Entity\CategorieArticle", cascade={"persist"})
 * @ORM\JoinColumn(referencedColumnName="id", nullable=false)
 */
private $categorie;
 
/**
 * ArticleBlog constructor.
 * @param \DateTime $created
 * @param \DateTime $updated
 */
public function __construct(\DateTime $created=null, \DateTime $updated=null)
{
    $this->created = $created;
    $this->updated = $updated;
}
 
/**
 * @ORM\preUpdate
 */
public function setUpdatedValue()
{
    $this->setUpdated(new \DateTime());
}
 
/**
 * Get id
 *
 * @return int
 */
public function getId()
{
    return $this->id;
}
 
/**
 * Set tittre
 *
 * @param string $tittre
 *
 * @return ArticleBlog
 */
public function setTittre($tittre)
{
    $this->tittre = $tittre;
 
    return $this;
}
 
/**
 * Get tittre
 *
 * @return string
 */
public function getTittre()
{
    return $this->tittre;
}
 
/**
 * Set auteur
 *
 * @param string $auteur
 *
 * @return ArticleBlog
 */
public function setAuteur($auteur)
{
    $this->auteur = $auteur;
 
    return $this;
}
 
/**
 * Get auteur
 *
 * @return string
 */
public function getAuteur()
{
    return $this->auteur;
}
 
/**
 * Set blog
 *
 * @param string $blog
 *
 * @return ArticleBlog
 */
public function setBlog($blog)
{
    $this->blog = $blog;
 
    return $this;
}
 
/**
 * Get blog
 *
 * @return string
 */
public function getBlog($length = null)
{
    if (false === is_null($length) && $length > 0)
        return substr($this->blog, 0, $length);
    else
        return $this->blog;
}
 
/**
 * Set image
 *
 * @param \Nioro\NioroBundle\Entity\MediaUpload $image
 * @return ArticleBlog
 */
public function setImage(\Nioro\NioroBundle\Entity\MediaUpload $image)
{
    $this->image = $image;
 
    return $this;
}
 
/**
 * Get image
 *
 * @return \Nioro\NioroBundle\Entity\MediaUpload
 */
public function getImage()
{
    return $this->image;
}
 
/**
 * Set created
 *
 * @param \DateTime $created
 *
 * @return ArticleBlog
 */
public function setCreated($created)
{
    $this->created = $created;
 
    return $this;
}
 
/**
 * Get created
 *
 * @return \DateTime
 */
public function getCreated()
{
    return $this->created;
}
 
/**
 * Set updated
 *
 * @param \DateTime $updated
 *
 * @return ArticleBlog
 */
public function setUpdated($updated)
{
    $this->updated = $updated;
 
    return $this;
}
 
/**
 * Get updated
 *
 * @return \DateTime
 */
public function getUpdated()
{
    return $this->updated;
}
 
/**
 * Set categorie
 *
 * @param \Nioro\NioroBundle\Entity\CategorieArticle $categorie
 *
 * @return ArticleBlog
 */
public function setCategorie(\Nioro\NioroBundle\Entity\CategorieArticle $categorie)
{
    $this->categorie = $categorie;
    return $this;
}
 
/**
 * Get categorie
 *
 * @return \Nioro\NioroBundle\Entity\CategorieArticle
 */
public function getCategorie()
{
    return $this->categorie;
}
}
MediaUploadType.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
 
class MediaUploadType extends AbstractType
{
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('file', FileType::class, array(
            'required' => true,
            'label' => 'Photo de couverture :',
            'attr' => array(
                'accept' => 'image/*',
            ))
        )
        ->add('name', TextType::class, array(
            'label' => 'Nom de l\'image',
            'required' => true,
            'attr' => array(
                'class' => 'form-control'
            )
        ));
}
 
/**
 * @param OptionsResolver $resolver
 */
public function setDefaultOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(
        array(
        'data_class' => 'Nioro\NioroBundle\Entity\MediaUpload'
    ));
}
 
/**
 * @return string
 */
public function getName()
{
    return 'nioro_niorobundle_mediaupload';
}
}
ArticleBlogType.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
 
class ArticleBlogType extends AbstractType
{
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('tittre', TextType::class, array(
            'required' => false,
            'attr' => array('class' => 'form-control')))
        ->add('auteur', TextType::class, array(
            'required' => false,
            'attr' => array('class' => 'form-control')))
        ->add('image', MediaUploadType::class)
        ->add('categorie', EntityType::class, array(
                    'class' => 'Nioro\NioroBundle\Entity\CategorieArticle',
                    'query_builder' => function(EntityRepository $q) {
                        return $q->createQueryBuilder('e')
                                 ->orderBy('e.libelle', 'ASC');
                    },
                    'choice_label' => 'libelle',
                )
        )
        ->add('blog', TextareaType::class, array(
                    'required' => false,
                    'attr' =>
                        array(
                            'class' => 'ckeditor',
                            'heidht' => '800px'
                )
            )
        )
        ->add('submit', SubmitType::class, array(
            'label' => 'Enregistrer article',
            'attr' =>
                array(
                    'class' => 'btn btn-info'
                )
            )
        );
}
 
/**
 * @param OptionsResolver $resolver
 */
public function setDefaultOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Nioro\NioroBundle\Entity\ArticleBlog'
    ));
}
 
/**
 * @return string
 */
public function getName()
{
    return 'nioro_niorobundle_articleblog';
}
}
ContribuerController.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
 
/**
 * Creates a new ArticleBlog entity.
 *
 */
public function createAction(Request $request)
{
    $entity = new ArticleBlog(new \DateTime(),new \DateTime());
    $form = $this->createForm(ArticleBlogType::class, $entity);
    $form->handleRequest($request);
    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();
        $request->get('session')->setFlash('info_1', 'Article bien enregistré');
        return $this->redirect($this->generateUrl('contrib_valider', array('id' => $entity->getId())));
    }
    return $this->render('NioroBundle:Default/Layout:contribuer.html.twig', array(
        'entity' => $entity,
        'form'   => $form->createView(),
    ));
}
contribuer.html.twig
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
 
{{ form_start(form) }}
    {{ form_widget(form) }}
{{ form_end(form) }}
Merci d'avance