Bonjour,
Je me heurte a un problème sur symfony2. J'ai beau chercher sur le net, je ne trouve rien qui explique comment résoudre ce problème. Enfin je n'ai pas trouvé.
Voilà, je dois créer un formulaire pour ajouter une nouvelle option et la liée à une classe et a des livres.
Le problème se situe apparemment au niveau du "liage".
J'utilise
Mais ça ne doit pas me retourner un objet comme le veut symfony2.
Code : Sélectionner tout - Visualiser dans une fenêtre à part ->add('classeclasse', 'entity', array('class' => 'LGB\BourseLivresBundle\Entity\Classe', 'property'=>'nom', 'expanded' => false, 'multiple' => false, 'required' => true, 'label'=>" "))
Je vous met l'érreur qu'il m'affiche mais je pense avoir cerné le problème qui est qu'il attend un objet Classe:
Si vous pouviez m'aider ça serait vraiment gentil(le) de votre part.Catchable Fatal Error: Argument 1 passed to LGB\BourseLivresBundle\Entity\ClasseHasOptions::setClasseclasse() must be an instance of Gen\entitiesBundle\Entity\Classe, instance of Doctrine\Common\Collections\ArrayCollection given, called in C:\wamp\www\Symfony\vendor\symfony\src\Symfony\Component\Form\Util\PropertyPath.php on line 347 and defined in C:\wamp\www\Symfony\src\LGB\BourseLivresBundle\Entity\ClasseHasOptions.php line 139
Pour cela je vous laisse différent passage de mon code dans le cas ou celà vient de celui-ci (surement n'est ce pas? sinon ça fonctionnerait)
Fichier AddClasseHasOptionsType.php:
GestionOptionController.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 class AddClasseHasOptionsType extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { $builder ->add('nomOption', 'text', array('read_only' => false , 'required' => true, 'property_path'=>false)) ->add('libelleOption', 'text', array('read_only'=>false, 'required' => true, 'property_path'=>false)) ->add('classeclasse', 'entity', array('class' => 'LGB\BourseLivresBundle\Entity\Classe', 'property'=>'nom', 'expanded' => false, 'multiple' => false, 'required' => true, 'label'=>" ")) ->add('livrelivre', 'entity', array('class' => 'LGB\BourseLivresBundle\Entity\Livre', 'property' => 'titre' , 'expanded' => true, 'multiple' => true, 'required' => true, 'label'=>" ")); } public function getName() { return 'lgb_bourselivresbundle_optiontype'; } public function getDefaultOptions(array $options) { return array( 'data_class' => 'LGB\BourseLivresBundle\Entity\ClasseHasOptions', ); } }
Ainsi qu'un bout du fichier twig ou j'appel ce formulaire:
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 public function gestionOptionAjouterAction() { // On crée un nouveau objet ClasseHasOption. $ClasseHasOptions = new ClasseHasOptions(); // On récupère le type crée dans AddOptionsType.php dans le dossier form. $form = $this->createForm(new AddClasseHasOptionsType(), $ClasseHasOptions); $request = $this->container->get('request'); if( $request->getMethod() == 'POST' ) { $form->bindRequest($request); if( $form->isValid() ) { $connect = $this->getDoctrine()->getEntityManager(); $postData = $connect->request->all(); $nomOptionString= $postData['form']['nomOption']; $libelleOptionString = $postData['form']['libelleOption']; if($nomOptionString != NULL && $libelleOptionString != NULL) { $Option=new Options(); $Option->setNomOption($nomOptionString); $Option->setLibelleOption($libelleOptionString); $connect->persist($Options); $ClasseHasOptions->setOptionsoption($Option); $connect->persist($ClasseHasOptions); $connect->flush(); } else { } // Envoie d'un message à la vue suivante // $this->get('session')->setFlash('info', 'L\'option a bien été créé !'); return $this->redirect( $this->generateUrl('\\ajouter') ); } } return $this->render('LGBBourseLivresBundle:Admin:ajouterOptions.html.twig', array( 'OptionsForm' => $form->createView(), )); }
Voila les 3 entités en questions:
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 <form action="" method="post" {{ form_enctype(OptionsForm) }}> <table style="width: 100%"> <tr> <td> {{ form_label(OptionsForm.nomOption, "Nom de l'option : ") }} {{ form_errors(OptionsForm.nomOption) }} {{ form_widget(OptionsForm.nomOption) }} </td> <td> {{ form_label(OptionsForm.libelleOption, "Libelle : ") }} {{ form_errors(OptionsForm.libelleOption) }} {{ form_widget(OptionsForm.libelleOption, {'attr': {'size': '50'}}) }} </td> </tr> </table> <table style="width: 100%"> <tr> <td valign=top style="width: 20%"><b> {{ form_label(OptionsForm.classeclasse, "Liste des classes :") }}</b><br><br> {{ form_errors(OptionsForm.classeclasse) }} {{ form_widget(OptionsForm.classeclasse, {'attr': {'class': 'checkbox'}}) }} </td> <td style="width: 80%"><b> {{ form_label(OptionsForm.livrelivre, "Liste des livres :") }}</b><br><br> {{ form_errors(OptionsForm.livrelivre) }} {{ form_widget(OptionsForm.livrelivre, {'attr': {'class': 'checkbox'}}) }} </td> </tr> </table> <p style="display:block;">{{ form_rest(OptionsForm) }}</p> <hr> <br> <p style="text-align:right;"><input type="submit" value="Ajouter l'option" class="btn-success" /></p> </form>
Entité ClasseHasOptions (celle sur laquelle je fait le formulaire vue que c'est elle qui est au centre de tout.):
Classe:
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 <?php namespace LGB\BourseLivresBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * Gen\entitiesBundle\Entity\ClasseHasOptions * * @ORM\Table(name="classe_has_options") * @ORM\Entity */ class ClasseHasOptions { /** * @var integer $classeIdclasse * * @ORM\Column(name="classe_idclasse", type="integer", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="NONE") */ private $classeIdclasse; /** * @var integer $optionsIdoption * * @ORM\Column(name="options_idoption", type="integer", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="NONE") */ private $optionsIdoption; /** * @var integer $livreIdlivre * * @ORM\Column(name="livre_idlivre", type="integer", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="NONE") */ private $livreIdlivre; /** * @var Classe * * @ORM\ManyToOne(targetEntity="Classe") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="classe_idclasse", referencedColumnName="idclasse") * }) */ private $classeclasse; /** * @var Options * * @ORM\ManyToOne(targetEntity="Options") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="options_idoption", referencedColumnName="idoption") * }) */ private $optionsoption; /** * @var Livre * * @ORM\ManyToOne(targetEntity="Livre") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="livre_idlivre", referencedColumnName="idlivre") * }) */ private $livrelivre; /** * Set classeIdclasse * * @param integer $classeIdclasse */ public function setClasseIdclasse($classeIdclasse) { $this->classeIdclasse = $classeIdclasse; } /** * Get classeIdclasse * * @return integer */ public function getClasseIdclasse() { return $this->classeIdclasse; } /** * Set optionsIdoption * * @param integer $optionsIdoption */ public function setOptionsIdoption($optionsIdoption) { $this->optionsIdoption = $optionsIdoption; } /** * Get optionsIdoption * * @return integer */ public function getOptionsIdoption() { return $this->optionsIdoption; } /** * Set livreIdlivre * * @param integer $livreIdlivre */ public function setLivreIdlivre($livreIdlivre) { $this->livreIdlivre = $livreIdlivre; } /** * Get livreIdlivre * * @return integer */ public function getLivreIdlivre() { return $this->livreIdlivre; } /** * Set classeclasse * * @param Gen\entitiesBundle\Entity\Classe $classeclasse */ public function setClasseclasse(\Gen\entitiesBundle\Entity\Classe $classeclasse) { $this->classeclasse = $classeclasse; } /** * Get classeclasse * * @return Gen\entitiesBundle\Entity\Classe */ public function getClasseclasse() { return $this->classeclasse; } /** * Set optionsoption * * @param Gen\entitiesBundle\Entity\Options $optionsoption */ public function setOptionsoption(\Gen\entitiesBundle\Entity\Options $optionsoption) { $this->optionsoption = $optionsoption; } /** * Get optionsoption * * @return Gen\entitiesBundle\Entity\Options */ public function getOptionsoption() { return $this->optionsoption; } /** * Set livrelivre * * @param Gen\entitiesBundle\Entity\Livre $livrelivre */ public function setLivrelivre(\Gen\entitiesBundle\Entity\Livre $livrelivre) { $this->livrelivre = $livrelivre; } /** * Get livrelivre * * @return Gen\entitiesBundle\Entity\Livre */ public function getLivrelivre() { return $this->livrelivre; } }
Et Livre:
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 <?php namespace LGB\BourseLivresBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * LGB\BourseLivresBundle\Entity\Classe * * @ORM\Table(name="classe") * @ORM\Entity(repositoryClass="LGB\BourseLivresBundle\Entity\ClasseRepository") */ class Classe { /** * @var integer $idclasse * * @ORM\Column(name="idclasse", type="integer", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") */ private $idclasse; /** * @var string $nom * * @ORM\Column(name="nom", type="string", length=10, nullable=false) */ private $nom; /** * @var string $libelleMef * * @ORM\Column(name="libelle_MEF", type="string", length=50, nullable=false) */ private $libelleMef; /** * @var Livre * * @ORM\ManyToMany(targetEntity="Livre", inversedBy="classeclasse") * @ORM\JoinTable(name="classe_has_livre", * joinColumns={ * @ORM\JoinColumn(name="classe_idclasse", referencedColumnName="idclasse") * }, * inverseJoinColumns={ * @ORM\JoinColumn(name="livre_idlivre", referencedColumnName="idlivre") * } * ) */ private $livrelivre; public function __construct() { $this->livrelivre = new \Doctrine\Common\Collections\ArrayCollection(); } /** * Get idclasse * * @return integer */ public function getIdclasse() { return $this->idclasse; } /** * Set nom * * @param string $nom */ public function setNom($nom) { $this->nom = $nom; } /** * Get nom * * @return string */ public function getNom() { return $this->nom; } /** * Set libelleMef * * @param string $libelleMef */ public function setLibelleMef($libelleMef) { $this->libelleMef = $libelleMef; } /** * Get libelleMef * * @return string */ public function getLibelleMef() { return $this->libelleMef; } /** * Add livrelivre * * @param LGB\BourseLivresBundle\Entity\Livre $livrelivre */ public function addLivre(\LGB\BourseLivresBundle\Entity\Livre $livrelivre) { $this->livrelivre[] = $livrelivre; } /** * Get livrelivre * * @return Doctrine\Common\Collections\Collection */ public function getLivrelivre() { return $this->livrelivre; } }
Je vous remercie de votre aide à l'avance.
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 <?php namespace LGB\BourseLivresBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * LGB\BourseLivresBundle\Entity\Livre * * @ORM\Table(name="livre") * @ORM\Entity(repositoryClass="LGB\BourseLivresBundle\Entity\LivreRepository") */ class Livre { /** * @var integer $idlivre * * @ORM\Column(name="idlivre", type="integer", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") */ private $idlivre; /** * @var string $discipline * * @ORM\Column(name="discipline", type="string", length=50, nullable=false) */ private $discipline; /** * @var string $titre * * @ORM\Column(name="titre", type="string", length=100, nullable=false) */ private $titre; /** * @var string $editeur * * @ORM\Column(name="editeur", type="string", length=50, nullable=false) */ private $editeur; /** * @var string $codeEan * * @ORM\Column(name="code_ean", type="string", length=20, nullable=false) */ private $codeEan; /** * @var decimal $prixPublic * * @ORM\Column(name="prix_public", type="decimal", nullable=false) */ private $prixPublic; /** * @var decimal $prixReprise * * @ORM\Column(name="prix_reprise", type="decimal", nullable=false) */ private $prixReprise; /** * @var decimal $prixVente * * @ORM\Column(name="prix_vente", type="decimal", nullable=false) */ private $prixVente; /** * @var integer $quantiteTotale * * @ORM\Column(name="quantite_totale", type="integer", nullable=true) */ private $quantiteTotale; /** * @var integer $quantiteSortie * * @ORM\Column(name="quantite_sortie", type="integer", nullable=true) */ private $quantiteSortie; /** * @var boolean $plusEdite * * @ORM\Column(name="plus_edite", type="boolean", nullable=false) */ private $plusEdite; /** * @var boolean $nonRepris * * @ORM\Column(name="non_repris", type="boolean", nullable=false) */ private $nonRepris; /** * @var Classe * * @ORM\ManyToMany(targetEntity="Classe", mappedBy="livrelivre") */ private $classeclasse; public function __construct() { $this->classeclasse = new \Doctrine\Common\Collections\ArrayCollection(); } /** * Get idlivre * * @return integer */ public function getIdlivre() { return $this->idlivre; } /** * Set discipline * * @param string $discipline */ public function setDiscipline($discipline) { $this->discipline = $discipline; } /** * Get discipline * * @return string */ public function getDiscipline() { return $this->discipline; } /** * Set titre * * @param string $titre */ public function setTitre($titre) { $this->titre = $titre; } /** * Get titre * * @return string */ public function getTitre() { return $this->titre; } /** * Set editeur * * @param string $editeur */ public function setEditeur($editeur) { $this->editeur = $editeur; } /** * Get editeur * * @return string */ public function getEditeur() { return $this->editeur; } /** * Set codeEan * * @param string $codeEan */ public function setCodeEan($codeEan) { $this->codeEan = $codeEan; } /** * Get codeEan * * @return string */ public function getCodeEan() { return $this->codeEan; } /** * Set prixPublic * * @param decimal $prixPublic */ public function setPrixPublic($prixPublic) { $this->prixPublic = $prixPublic; } /** * Get prixPublic * * @return decimal */ public function getPrixPublic() { return $this->prixPublic; } /** * Set prixReprise * * @param decimal $prixReprise */ public function setPrixReprise($prixReprise) { $this->prixReprise = $prixReprise; } /** * Get prixReprise * * @return decimal */ public function getPrixReprise() { return $this->prixReprise; } /** * Set prixVente * * @param decimal $prixVente */ public function setPrixVente($prixVente) { $this->prixVente = $prixVente; } /** * Get prixVente * * @return decimal */ public function getPrixVente() { return $this->prixVente; } /** * Set quantiteTotale * * @param integer $quantiteTotale */ public function setQuantiteTotale($quantiteTotale) { $this->quantiteTotale = $quantiteTotale; } /** * Get quantiteTotale * * @return integer */ public function getQuantiteTotale() { return $this->quantiteTotale; } /** * Set quantiteSortie * * @param integer $quantiteSortie */ public function setQuantiteSortie($quantiteSortie) { $this->quantiteSortie = $quantiteSortie; } /** * Get quantiteSortie * * @return integer */ public function getQuantiteSortie() { return $this->quantiteSortie; } /** * Set plusEdite * * @param boolean $plusEdite */ public function setPlusEdite($plusEdite) { $this->plusEdite = $plusEdite; } /** * Get plusEdite * * @return boolean */ public function getPlusEdite() { return $this->plusEdite; } /** * Set nonRepris * * @param boolean $nonRepris */ public function setNonRepris($nonRepris) { $this->nonRepris = $nonRepris; } /** * Get nonRepris * * @return boolean */ public function getNonRepris() { return $this->nonRepris; } /** * Add classeclasse * * @param LGB\BourseLivresBundle\Entity\Classe $classeclasse */ public function addClasse(\LGB\BourseLivresBundle\Entity\Classe $classeclasse) { $this->classeclasse[] = $classeclasse; } /** * Get classeclasse * * @return Doctrine\Common\Collections\Collection */ public function getClasseclasse() { return $this->classeclasse; } }
Partager