Bonjour,

Je suis un petit nouveau dans Symfony j'ai un soucis sur le fait que j'arrive très bien à récupérer mes résultats de requêtes dans une vue twig :

Exemple, vue "liste de commandes" m'affiche bien toute mes commandes, ou "voir une seule commande" je passe en paramètre GET mon id de commande est ça roule.

En revanche j'aimerais pouvoir afficher tous les commandes pour un seul utilisateur qui est connecté à ma session (session qui contient l'id_utilisateur) , en gros j'aimerais pouvoir attaquer le repository et y mettre l'id_utilisateur en paramètre,
afin que le repository me sort le résultat d'une requête du type : "SELECT * FROM commande WHERE id_client = id_utilisateur_de_la_session";

un peu d'extrait de code :

Entity Commande

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
 
<?php
 
namespace App\Entity;
 
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
 
/**
 * @ORM\Entity(repositoryClass="App\Repository\CommandeRepository")
 */
class Commande
{
 
    public function __construct()
    {
        $this->dateCommande = new \DateTime('now');
    }
 
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;
 
    /**
     * @ORM\Column(type="integer", nullable=false)
     */
    private $id_client;
 
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $saisiPar;
 
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $Ville;
 
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $cimetiere;
 
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $emplacementDivision;
 
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $rang;
 
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $caser;
 
    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $numeroCons;
 
    /**
     * @ORM\Column(type="boolean")
     */
    private $sansDivision;
 
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $nomDuDefunt;
 
    /**
     * @ORM\Column(type="text", nullable=true)
     */
    private $gravures;
 
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $joindreDemandeTravaux;
 
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $joindreImage;
 
    /**
     * @ORM\Column(type="boolean", nullable=true)
     */
    private $orOuPeinture;
 
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $emplacementGravure;
 
    /**
     * @ORM\Column(type="datetime")
     */
    private $dateCommande;
 
    /**
     * @ORM\Column(type="boolean")
     */
    private $toutesLesLettres;
 
    /**
     * @ORM\Column(type="text", nullable=true)
     */
    private $autrePrecisez;
 
    /**
     * @ORM\Column(type="boolean")
     */
    private $motif;
 
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $autreCouleurPrecisez;
 
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $detailCommande;
 
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $detailTravaux;
 
    private $file;
 
    // On ajoute cet attribut pour y stocker le nom du fichier temporairement
    private $tempFilename;
 
    public function getId(): ?int
    {
        return $this->id;
    }
 
    public function getSaisiPar(): ?string
    {
        return $this->saisiPar;
    }
 
    public function setSaisiPar(?string $saisiPar): self
    {
        $this->saisiPar = $saisiPar;
 
        return $this;
    }
 
    public function getVille(): ?string
    {
        return $this->Ville;
    }
 
    public function setVille(?string $Ville): self
    {
        $this->Ville = $Ville;
 
        return $this;
    }
 
    public function getCimetiere(): ?string
    {
        return $this->cimetiere;
    }
 
    public function setCimetiere(?string $cimetiere): self
    {
        $this->cimetiere = $cimetiere;
 
        return $this;
    }
 
    public function getEmplacementDivision(): ?string
    {
        return $this->emplacementDivision;
    }
 
    public function setEmplacementDivision(?string $emplacementDivision): self
    {
        $this->emplacementDivision = $emplacementDivision;
 
        return $this;
    }
 
    public function getRang(): ?string
    {
        return $this->rang;
    }
 
    public function setRang(?string $rang): self
    {
        $this->rang = $rang;
 
        return $this;
    }
 
    public function getCaser(): ?string
    {
        return $this->caser;
    }
 
    public function setCaser(?string $caser): self
    {
        $this->caser = $caser;
 
        return $this;
    }
 
    public function getNumeroCons(): ?int
    {
        return $this->numeroCons;
    }
 
    public function setNumeroCons(?int $numeroCons): self
    {
        $this->numeroCons = $numeroCons;
 
        return $this;
    }
 
    public function getSansDivision(): ?bool
    {
        return $this->sansDivision;
    }
 
    public function setSansDivision(bool $sansDivision): self
    {
        $this->sansDivision = $sansDivision;
 
        return $this;
    }
 
    public function getNomDuDefunt(): ?string
    {
        return $this->nomDuDefunt;
    }
 
    public function setNomDuDefunt(?string $nomDuDefunt): self
    {
        $this->nomDuDefunt = $nomDuDefunt;
 
        return $this;
    }
 
    public function getGravures(): ?string
    {
        return $this->gravures;
    }
 
    public function setGravures(?string $gravures): self
    {
        $this->gravures = $gravures;
 
        return $this;
    }
 
    public function getJoindreDemandeTravaux(): ?string
    {
        return $this->joindreDemandeTravaux;
    }
 
    public function setJoindreDemandeTravaux(?string $joindreDemandeTravaux): self
    {
        $this->joindreDemandeTravaux = $joindreDemandeTravaux;
 
        return $this;
    }
 
    public function getJoindreImage(): ?string
    {
        return $this->joindreImage;
    }
 
    public function setJoindreImage(?string $joindreImage): self
    {
        $this->joindreImage = $joindreImage;
 
        return $this;
    }
 
    public function getOrOuPeinture(): ?bool
    {
        return $this->orOuPeinture;
    }
 
    public function setOrOuPeinture(?bool $orOuPeinture): self
    {
        $this->orOuPeinture = $orOuPeinture;
 
        return $this;
    }
 
    public function getEmplacementGravure(): ?string
    {
        return $this->emplacementGravure;
    }
 
    public function setEmplacementGravure(?string $emplacementGravure): self
    {
        $this->emplacementGravure = $emplacementGravure;
 
        return $this;
    }
 
    public function getDateCommande(): ?\DateTimeInterface
    {
        return $this->dateCommande;
    }
 
    public function setDateCommande(\DateTimeInterface $dateCommande): self
    {
        $this->dateCommande = $dateCommande;
 
        return $this;
    }
 
    public function getToutesLesLettres(): ?bool
    {
        return $this->toutesLesLettres;
    }
 
    public function setToutesLesLettres(bool $toutesLesLettres): self
    {
        $this->toutesLesLettres = $toutesLesLettres;
 
        return $this;
    }
 
    public function getAutrePrecisez(): ?string
    {
        return $this->autrePrecisez;
    }
 
    public function setAutrePrecisez(?string $autrePrecisez): self
    {
        $this->autrePrecisez = $autrePrecisez;
 
        return $this;
    }
 
    public function getMotif(): ?bool
    {
        return $this->motif;
    }
 
    public function setMotif(bool $motif): self
    {
        $this->motif = $motif;
 
        return $this;
    }
 
    public function getAutreCouleurPrecisez(): ?string
    {
        return $this->autreCouleurPrecisez;
    }
 
    public function setAutreCouleurPrecisez(?string $autreCouleurPrecisez): self
    {
        $this->autreCouleurPrecisez = $autreCouleurPrecisez;
 
        return $this;
    }
 
    public function getDetailCommande(): ?string
    {
        return $this->detailCommande;
    }
 
    public function setDetailCommande(?string $detailCommande): self
    {
        $this->detailCommande = $detailCommande;
 
        return $this;
    }
 
    public function getDetailTravaux(): ?string
    {
        return $this->detailTravaux;
    }
 
    public function setDetailTravaux(?string $detailTravaux): self
    {
        $this->detailTravaux = $detailTravaux;
 
        return $this;
    }
 
    /**
     * @return mixed
     */
    public function getIdClient()
    {
        return $this->id_client;
    }
 
    /**
     * @param mixed $id_client
     */
    public function setIdClient($id_client): void
    {
        $this->id_client = $id_client;
    }
 
    //ESPACE POUR GERER LENVOIE DE FICHIER
    // On modifie le setter de File, pour prendre en compte l'upload d'un fichier lorsqu'il en existe déjà un autre
    public function setFile(UploadedFile $file)
    {
        $this->file = $file;
 
        // On vérifie si on avait déjà un fichier pour cette entité
        if (null !== $this->url) {
            // On sauvegarde l'extension du fichier pour le supprimer plus tard
            $this->tempFilename = $this->url;
 
            // On réinitialise les valeurs des attributs url et alt
            $this->url = null;
            $this->alt = null;
        }
    }
 
    /**
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function preUpload()
    {
        // Si jamais il n'y a pas de fichier (champ facultatif), on ne fait rien
        if (null === $this->file) {
            return;
        }
 
        // Le nom du fichier est son id, on doit juste stocker également son extension
        // Pour faire propre, on devrait renommer cet attribut en « extension », plutôt que « url »
        $this->url = $this->file->guessExtension();
 
        // Et on génère l'attribut alt de la balise <img>, à la valeur du nom du fichier sur le PC de l'internaute
        $this->alt = $this->file->getClientOriginalName();
    }
 
    /**
     * @ORM\PostPersist()
     * @ORM\PostUpdate()
     */
    public function upload()
    {
        // Si jamais il n'y a pas de fichier (champ facultatif), on ne fait rien
        if (null === $this->file) {
            return;
        }
 
        // Si on avait un ancien fichier, on le supprime
        if (null !== $this->tempFilename) {
            $oldFile = $this->getUploadRootDir().'/'.$this->id.'.'.$this->tempFilename;
            if (file_exists($oldFile)) {
                unlink($oldFile);
            }
        }
 
        // On déplace le fichier envoyé dans le répertoire de notre choix
        $this->file->move(
            $this->getUploadRootDir(), // Le répertoire de destination
            $this->id.'.'.$this->url   // Le nom du fichier à créer, ici « id.extension »
        );
    }
 
    /**
     * @ORM\PreRemove()
     */
    public function preRemoveUpload()
    {
        // On sauvegarde temporairement le nom du fichier, car il dépend de l'id
        $this->tempFilename = $this->getUploadRootDir().'/'.$this->id.'.'.$this->url;
    }
 
    /**
     * @ORM\PostRemove()
     */
    public function removeUpload()
    {
        // En PostRemove, on n'a pas accès à l'id, on utilise notre nom sauvegardé
        if (file_exists($this->tempFilename)) {
            // On supprime le fichier
            unlink($this->tempFilename);
        }
    }
 
    public function getUploadDir()
    {
        // On retourne le chemin relatif vers l'image pour un navigateur
        return 'uploads/img';
    }
 
    protected function getUploadRootDir()
    {
        // On retourne le chemin relatif vers l'image pour notre code PHP
        return __DIR__.'/../../../../web/'.$this->getUploadDir();
    }
}
Mon CommandeRepository qui contient ma méthode findByNumClient($id_client) mais que je ne sais comment appeler et récupérer le résultat dans ma vue twig.

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
 
<?php
 
namespace App\Repository;
 
use App\Entity\Commande;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;
 
/**
 * @method Commande|null find($id, $lockMode = null, $lockVersion = null)
 * @method Commande|null findOneBy(array $criteria, array $orderBy = null)
 * @method Commande[]    findAll()
 * @method Commande[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class CommandeRepository extends ServiceEntityRepository
{
    public function __construct(RegistryInterface $registry)
    {
        parent::__construct($registry, Commande::class);
    }
 
    /**
     * @param $id_client
     * @return mixed
     */
    public function findByNumClient($id_client)
    {
        return $this->createQueryBuilder('c')
            ->andWhere('c.id_client = :val')
            ->setParameter('val', $id_client)
            ->orderBy('c.id', 'ASC')
            ->getQuery()
            ->getResult()
            ;
    }
 
    // /**
    //  * @return Commande[] Returns an array of Commande objects
    //  */
    /*
    public function findByExampleField($value)
    {
        return $this->createQueryBuilder('c')
            ->andWhere('c.exampleField = :val')
            ->setParameter('val', $value)
            ->orderBy('c.id', 'ASC')
            ->setMaxResults(10)
            ->getQuery()
            ->getResult()
        ;
    }
    */
 
    /*
    public function findOneBySomeField($value): ?Commande
    {
        return $this->createQueryBuilder('c')
            ->andWhere('c.exampleField = :val')
            ->setParameter('val', $value)
            ->getQuery()
            ->getOneOrNullResult()
        ;
    }
    */
}

Mon contrôleur CommandeController dans lequel je devrais sans doute pouvoir appeler la méthode de mon repository si j'ai pigé symfony lol.

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
 
<?php
 
namespace App\Controller;
 
use App\Entity\Commande;
use App\Form\CommandeType;
use App\Repository\CommandeRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
 
/**
 * @Route("/commande")
 */
class CommandeController extends AbstractController
{
    /**
     * @Route("/admin/", name="commande_index", methods={"GET"})
     */
    public function index(CommandeRepository $commandeRepository): Response
    {
        return $this->render('commande/index.html.twig', [
            'commandes' => $commandeRepository->findAll(),
        ]);
    }
 
    /**
     * @Route("/member/new", name="commande_new", methods={"GET","POST"})
     */
    public function new(Request $request): Response
    {
        $commande = new Commande();
        $form = $this->createForm(CommandeType::class, $commande);
        $form->handleRequest($request);
 
        if ($form->isSubmitted() && $form->isValid()) {
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($commande);
            $entityManager->flush();
 
            return $this->redirectToRoute('commande_index');
        }
 
        return $this->render('commande/new.html.twig', [
            'commande' => $commande,
            'form' => $form->createView(),
        ]);
    }
 
    /**
     * @Route("/member/{id}", name="commande_show", methods={"GET"})
     */
    public function show(Commande $commande): Response
    {
        return $this->render('commande/show.html.twig', [
            'commande' => $commande,
        ]);
    }
 
    /**
     * @Route("/member/{id}/edit", name="commande_edit", methods={"GET","POST"})
     */
    public function edit(Request $request, Commande $commande): Response
    {
        $form = $this->createForm(CommandeType::class, $commande);
        $form->handleRequest($request);
 
        if ($form->isSubmitted() && $form->isValid()) {
            $this->getDoctrine()->getManager()->flush();
 
            return $this->redirectToRoute('commande_index', [
                'id' => $commande->getId(),
            ]);
        }
 
        return $this->render('commande/edit.html.twig', [
            'commande' => $commande,
            'form' => $form->createView(),
        ]);
    }
 
    /**
     * @Route("/admin/{id}", name="commande_delete", methods={"DELETE"})
     */
    public function delete(Request $request, Commande $commande): Response
    {
        if ($this->isCsrfTokenValid('delete'.$commande->getId(), $request->request->get('_token'))) {
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->remove($commande);
            $entityManager->flush();
        }
 
        return $this->redirectToRoute('commande_index');
    }
 
 
}
Ma vue twig show.html.twig

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
 
{% extends 'base.html.twig' %}
 
{% block title %}Commande{% endblock %}
 
{% block body %}
    <h1>Commande</h1>
 
    <table class="table">
        <tbody>
            <tr>
                <th>Numéro client</th>
                <td>{{ commande.idClient }}</td>
            </tr>
            <tr>
                <th>Id</th>
                <td>{{ commande.id }}</td>
            </tr>
            <tr>
                <th>SaisiPar</th>
                <td>{{ commande.saisiPar }}</td>
            </tr>
            <tr>
                <th>Ville</th>
                <td>{{ commande.Ville }}</td>
            </tr>
            <tr>
                <th>Cimetiere</th>
                <td>{{ commande.cimetiere }}</td>
            </tr>
            <tr>
                <th>EmplacementDivision</th>
                <td>{{ commande.emplacementDivision }}</td>
            </tr>
            <tr>
                <th>Rang</th>
                <td>{{ commande.rang }}</td>
            </tr>
            <tr>
                <th>Caser</th>
                <td>{{ commande.caser }}</td>
            </tr>
            <tr>
                <th>NumeroCons</th>
                <td>{{ commande.numeroCons }}</td>
            </tr>
            <tr>
                <th>SansDivision</th>
                <td>{{ commande.sansDivision ? 'Yes' : 'No' }}</td>
            </tr>
            <tr>
                <th>NomDuDefunt</th>
                <td>{{ commande.nomDuDefunt }}</td>
            </tr>
            <tr>
                <th>Gravures</th>
                <td>{{ commande.gravures }}</td>
            </tr>
            <tr>
                <th>JoindreDemandeTravaux</th>
                <td>{{ commande.joindreDemandeTravaux }}</td>
            </tr>
            <tr>
                <th>JoindreImage</th>
                <td>{{ commande.joindreImage }}</td>
            </tr>
            <tr>
                <th>OrOuPeinture</th>
                <td>{{ commande.orOuPeinture ? 'Yes' : 'No' }}</td>
            </tr>
            <tr>
                <th>EmplacementGravure</th>
                <td>{{ commande.emplacementGravure }}</td>
            </tr>
            <tr>
                <th>DateCommande</th>
                <td>{{ commande.dateCommande ? commande.dateCommande|date('Y-m-d H:i:s') : '' }}</td>
            </tr>
            <tr>
                <th>ToutesLesLettres</th>
                <td>{{ commande.toutesLesLettres ? 'Yes' : 'No' }}</td>
            </tr>
            <tr>
                <th>AutrePrecisez</th>
                <td>{{ commande.autrePrecisez }}</td>
            </tr>
            <tr>
                <th>Motif</th>
                <td>{{ commande.motif ? 'Yes' : 'No' }}</td>
            </tr>
            <tr>
                <th>AutreCouleurPrecisez</th>
                <td>{{ commande.autreCouleurPrecisez }}</td>
            </tr>
            <tr>
                <th>DetailCommande</th>
                <td>{{ commande.detailCommande }}</td>
            </tr>
            <tr>
                <th>DetailTravaux</th>
                <td>{{ commande.detailTravaux }}</td>
            </tr>
        </tbody>
    </table>
 
    <a href="{{ path('commande_index') }}">back to list</a>
 
    <a href="{{ path('commande_edit', {'id': commande.id}) }}">edit</a>
 
    {%  if is_granted('ROLE_ADMIN') %}
        {{ include('commande/_delete_form.html.twig') }}
    {% endif %}
{% endblock %}
Help me plz