Salut tout le monde. c'est encore un plaisir pour moi de toujours compter sur vous pour palier a mes difficultes
En fait je travaille sur un site de E-Commerce et je suis bloque depuis un bon bout de temps. Mon probleme est le suivant :
J'ai en session un panier que peut contenir plusieurs ligne de produit, ce qui constitue une commande. je voudrai donc arriver a la
validation et enregistrement de la commande dans la base de donnees, mais j'y arrive pas. mon code est le suivant.
Voici mes entites, Commandes, CommandeArticle, Articles
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 /** * @Route("/commande", name="commande", methods={"GET"}) */ public function validationCommandeAction(Request $request) { $session = $request->getSession(); $commande = new Commande(); if (!($session->has('cart'))) { //$session->set('cart', array()); return $this->redirectToRoute('cart'); } $cart = $session->get('cart'); $em = $this->getDoctrine()->getManager(); $articles = $em->getRepository(Article::class)->findBy(array_keys($cart)); //$articles = $em->getRepository(Article::class)->getArray(array_keys($cart)); $cartCmd = []; $totalNet = 0; foreach ($articles as $article) { $totalU = ($article->getPrixUnitaire() * $cart[$article->getId()]); $totalNet += floatval($totalU); $cartCmd[$article->getId()] = [ 'ref' => $article->getReference(), 'designation' => $article->getDesignation(), 'prixU' => $article->getPrixUnitaire(), 'quantite' => intval($cart[$article->getId()]), 'totalU' => $totalU,]; $cmdArticle = new CommandeArticle(); $cmdArticle->setQuantite($cart[$article->getId()]); $article->setQuantite(($article->getQuantite()) - ($cart[$article->getId()])); $cmdArticle->setArticle($article->getId()); $cmdArticle->setCommande($commande); $em->persist($cmdArticle); } $commande->setCartCmd($cartCmd); $commande->setMontant($totalNet); $commande->setCreatedAt(new \DateTime()); $em->persist($commande); $em->flush(); $session->remove('cart'); $request->getSession()->getFlashBag()->add('success', 'Commande validée avec succès'); return $this->redirectToRoute('default'); }
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 <?php namespace App\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity(repositoryClass="App\Repository\ArticleRepository") */ class Article { /** * @ORM\Id() * @ORM\GeneratedValue() * @ORM\Column(type="integer") */ private $id; /** * @ORM\Column(type="string", length=255) */ private $Designation; /** * @ORM\Column(type="string", length=255) */ private $Reference; /** * @ORM\Column(type="float") */ private $PrixUnitaire; /** * @ORM\Column(type="integer", nullable=true) */ private $Quantite; /** * @ORM\Column(type="string", length=255) */ private $ImageArticle; /** * @ORM\Column(type="string", length=255) */ private $Description; /** * @ORM\Column(type="datetime") */ private $CreatedAt; /** * @ORM\ManyToOne(targetEntity="App\Entity\Categorie", inversedBy="articles") */ private $Category; /** * @ORM\Column(type="string", length=255) */ private $Image2; /** * @ORM\Column(type="string", length=255) */ private $Image3; /** * @ORM\Column(type="string", length=255, nullable=true) */ private $Poids; /** * @ORM\Column(type="string", length=255, nullable=true) */ private $Dimensions; /** * @ORM\Column(type="string", length=255, nullable=true) */ private $Couleur; /** * @ORM\Column(type="string", length=255, nullable=true) */ private $Matiere; /** * @ORM\Column(type="string", length=255, nullable=true) */ private $Taille; /** * @ORM\OneToMany(targetEntity="App\Entity\CommandeArticle", mappedBy="Article") */ private $commandeArticles; public function __construct() { $this->commandeArticles = new ArrayCollection(); } public function getId(): ?int { return $this->id; } public function getDesignation(): ?string { return $this->Designation; } public function setDesignation(string $Designation): self { $this->Designation = $Designation; return $this; } public function getPrixUnitaire(): ?float { return $this->PrixUnitaire; } public function setPrixUnitaire(float $PrixUnitaire): self { $this->PrixUnitaire = $PrixUnitaire; return $this; } public function getQuantite(): ?int { return $this->Quantite; } public function setQuantite(?int $Quantite): self { $this->Quantite = $Quantite; return $this; } public function getImageArticle() { return $this->ImageArticle; } public function setImageArticle($ImageArticle) { $this->ImageArticle = $ImageArticle; return $this; } public function getDescription(): ?string { return $this->Description; } public function setDescription(string $Description): self { $this->Description = $Description; return $this; } public function getCreatedAt(): ?\DateTimeInterface { return $this->CreatedAt; } public function setCreatedAt(\DateTimeInterface $CreatedAt): self { $this->CreatedAt = $CreatedAt; return $this; } public function getCategory(): ?Categorie { return $this->Category; } public function setCategory(?Categorie $Category): self { $this->Category = $Category; return $this; } public function getImage2() { return $this->Image2; } public function setImage2($Image2) { $this->Image2 = $Image2; return $this; } public function getImage3() { return $this->Image3; } public function setImage3($Image3) { $this->Image3 = $Image3; return $this; } public function getReference(): ?string { return $this->Reference; } public function setReference(string $Reference): self { $this->Reference = $Reference; return $this; } public function getPoids(): ?string { return $this->Poids; } public function setPoids(?string $Poids): self { $this->Poids = $Poids; return $this; } public function getDimensions(): ?string { return $this->Dimensions; } public function setDimensions(?string $Dimensions): self { $this->Dimensions = $Dimensions; return $this; } public function getCouleur(): ?string { return $this->Couleur; } public function setCouleur(?string $Couleur): self { $this->Couleur = $Couleur; return $this; } public function getMatiere(): ?string { return $this->Matiere; } public function setMatiere(?string $Matiere): self { $this->Matiere = $Matiere; return $this; } public function getTaille(): ?string { return $this->Taille; } public function setTaille(?string $Taille): self { $this->Taille = $Taille; return $this; } /** * @return Collection|CommandeArticle[] */ public function getCommandeArticles(): Collection { return $this->commandeArticles; } public function addCommandeArticle(CommandeArticle $commandeArticle): self { if (!$this->commandeArticles->contains($commandeArticle)) { $this->commandeArticles[] = $commandeArticle; $commandeArticle->setArticle($this); } return $this; } public function removeCommandeArticle(CommandeArticle $commandeArticle): self { if ($this->commandeArticles->contains($commandeArticle)) { $this->commandeArticles->removeElement($commandeArticle); // set the owning side to null (unless already changed) if ($commandeArticle->getArticle() === $this) { $commandeArticle->setArticle(null); } } return $this; } }
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 <?php namespace App\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity(repositoryClass="App\Repository\CommandeRepository") */ class Commande { /** * @ORM\Id() * @ORM\GeneratedValue() * @ORM\Column(type="integer") */ private $id; /** * @ORM\Column(type="integer") */ private $Numero; /** * @ORM\Column(type="float") */ private $Montant; /** * @ORM\Column(type="datetime") */ private $CreatedAt; /** * @ORM\OneToMany(targetEntity="App\Entity\CommandeArticle", mappedBy="Commande") */ private $commandeArticles; /** * @ORM\Column(type="array", nullable=true) */ private $CartCmd = []; public function __construct() { $this->commandeArticles = new ArrayCollection(); } public function getId(): ?int { return $this->id; } public function getNumero(): ?int { return $this->Numero; } public function setNumero(int $Numero): self { $this->Numero = $Numero; return $this; } public function getMontant(): ?float { return $this->Montant; } public function setMontant(float $Montant): self { $this->Montant = $Montant; return $this; } public function getCreatedAt(): ?\DateTimeInterface { return $this->CreatedAt; } public function setCreatedAt(\DateTimeInterface $CreatedAt): self { $this->CreatedAt = $CreatedAt; return $this; } /** * @return Collection|CommandeArticle[] */ public function getCommandeArticles(): Collection { return $this->commandeArticles; } public function addCommandeArticle(CommandeArticle $commandeArticle): self { if (!$this->commandeArticles->contains($commandeArticle)) { $this->commandeArticles[] = $commandeArticle; $commandeArticle->setCommande($this); } return $this; } public function removeCommandeArticle(CommandeArticle $commandeArticle): self { if ($this->commandeArticles->contains($commandeArticle)) { $this->commandeArticles->removeElement($commandeArticle); // set the owning side to null (unless already changed) if ($commandeArticle->getCommande() === $this) { $commandeArticle->setCommande(null); } } return $this; } public function getCartCmd(): ?array { return $this->CartCmd; } public function setCartCmd(?array $CartCmd): self { $this->CartCmd = $CartCmd; return $this; } }
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 <?php namespace App\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity(repositoryClass="App\Repository\CommandeArticleRepository") */ class CommandeArticle { /** * @ORM\Id() * @ORM\GeneratedValue() * @ORM\Column(type="integer") */ private $id; /** * @ORM\Column(type="integer") */ private $Quantite; /** * @ORM\ManyToOne(targetEntity="App\Entity\Commande", inversedBy="commandeArticles") */ private $Commande; /** * @ORM\ManyToOne(targetEntity="App\Entity\Article", inversedBy="commandeArticles") */ private $Article; public function __construct() { $this->Article = new ArrayCollection(); } public function getId(): ?int { return $this->id; } public function getQuantite(): ?int { return $this->Quantite; } public function setQuantite(int $Quantite): self { $this->Quantite = $Quantite; return $this; } public function getCommande(): ?Commande { return $this->Commande; } public function setCommande(?Commande $Commande): self { $this->Commande = $Commande; return $this; } public function getArticle(): ?Article { return $this->Article; } public function setArticle(?Article $Article): self { $this->Article = $Article; return $this; } }
Partager