Symfony 5 Affichage d'une collection

Par Rostand Ngagoum, il y a 4 ans


Bonjour,

je recupère les données d'un "participant" qui a une relation OneToMany avec "coursses" avec la requet suivante.

ici mon ParticipantRepository.php qui me permet d'extrait les données de ma BD

/** * Récupère les participants en lien avec la recherche * @return Participant[] */ public function findSearch2(ParticipantSearchData $search): array { $query = $this->createQueryBuilder('p') ->select('p', 'c') ->join('p.courses', 'c'); if (!empty($search->p)) { $query = $query ->andWhere('p.id = :p') ->orderBy('c.id', 'DESC') ->setMaxResults(1) ->setParameter('p', $search->p); } return $query->getQuery()->getResult(); }

ici mon controller

if (!empty($participantsSearchForm->isSubmitted())) { $participantSearch = $participantsRepo->findSearch2($data); } else { $participantSearch = null ; } dump($participantSearch); return $this->render('participant/index.html.twig', [ 'participantSearchForm' => $participantSearchForm->createView(), 'participantForm' => $participantForm->createView(), 'participantsSearch' => $participantSearch, 'editMode' => $participants->getId() !== null ]); }

ici les infos de mon dump($participantSearch);

array:1 [▼ 0 => App\Entity\Participant {#1502 ▼ -id: 10 -names: "Mathias Keebler" -contact: 651286626 -language: "Anglais" -type: "Jeunes intensif" -slug: "mathias-keebler" -regDate: DateTime @1633867465 {#1501 ▶} -courses: Doctrine\ORM\PersistentCollection {#1454 ▼ -snapshot: array:1 [ …1] -owner: App\Entity\Participant {#1502} -association: array:15 [ …15] -em: Doctrine\ORM\EntityManager {#332 …11} -backRefFieldName: "participants" -typeClass: Doctrine\ORM\Mapping\ClassMetadata {#1397 …} -isDirty: false #collection: Doctrine\Common\Collections\ArrayCollection {#1452 ▼ -elements: array:1 [▼ 0 => App\Entity\Course {#1337 ▼ -id: 727 -level: "Avancé" -module: "A" -moduleNum: 4 -classDays: " Le Mercredi et Vendredi" -classTimes: "14H - 16H" -classRoom: "E" -libraryDay: "vendredi" -period: "Term 4" -payType: "Totalité" -payDescription: "A payer les frais d'inscription en totalité" -payReceived: 25000 -regDate: DateTime @1615658277 {#1150 ▶} -participants: App\Entity\Participant {#1502} -reports: null } ] } #initialized: true } } ]

Ce que je veux

je veux afficher en vu twig le "names" du participant sur App\Entity\Participant et les elements de courses qui se trouvent sur App\Entity\Course .

Merci d'avance pour ceux qui prendront le temps de me repondre.

5 réponses

gillesr, il y a 4 ans

Bonjour,
tu as passé 'participantsSearch' à ta vue donc dans ton fichier 'participant/index.html.twig' tu dois pouvoir afficher les infos avec un for ... in :

{% for participant in participantsSearch %} <h2>{{ participant.names }}</h2> <ul> {% for course in participant.courses.elements %} <li>{{ course.id }} {{ course.module }}</li> {% enfor %} </ul> {% endfor %}
Rostand Ngagoum, il y a 4 ans

Bonjour gillesr et merci pour ta réponse

j'ai fait comme ta dis

<table class="table table-sm table-striped m-0"> <thead> <tr> <th scope="col">Noms et Prénoms</th> <th scope="col">Période</th> <th scope="col">Niveau</th> <th scope="col">Jours</th> <th scope="col">Heures</th> <th scope="col">Salle</th> <th scope="col">Action</th> </tr> </thead> <tbody> {% for participant in participantsSearch %} <tr class="table-light"> <td>{{ participant.names }}</td> {% for course in participant.courses.elements %} <td>{{ course.period }} - {{ course.regDate|date("Y") }} </td> <td>{{ course.level }} {{course.module }}{{course.moduleNum }}</td> <td>{{ course.classDays }}</td> <td>{{ course.classTimes }}</td> <td>{{ course.payReceived }} FCFA</td> <td> <a class="btn btn-primary btn-sm" href="{{path('reportReel_create', {'id': course.id })}}"><i class="fas fa-plus me-1"></i>Bulletin</a> </td> </tr> {% endfor %} {% endfor %} </tbody> </table>

Mais j'ai cette erreur:

Neither the property "elements" nor one of the methods "elements()", "getelements()"/"iselements()"/"haselements()" or "__call()" exist and have public access in class "Doctrine\ORM\PersistentCollection".
olive140, il y a 4 ans

{% for course in participant.courses %}

Rostand Ngagoum, il y a 4 ans

Bonjour olive140, Merci pour ta répose, ça marche! je parviens à tout affiché avec le code suivant:

<table class="table table-sm table-striped m-0"> <thead> <tr> <th scope="col">Noms et Prénoms</th> <th scope="col">Période</th> <th scope="col">Niveau</th> <th scope="col">Jours</th> <th scope="col">Heures</th> <th scope="col">Salle</th> <th scope="col">Action</th> </tr> </thead> <tbody> {% for participant in participantSearch %} <tr class="table-light"> <td>{{ participant.names }}</td> {% for course in participant.courses %} <td>{{ course.period }} - {{ course.regDate|date("Y") }} </td> <td>{{ course.level }} {{course.module }}{{course.moduleNum }}</td> <td>{{ course.classDays }}</td> <td>{{ course.classTimes }}</td> <td>{{ course.payReceived }} FCFA</td> <td> <a class="btn btn-primary btn-sm" href="{{path('reportReel_create', {'id_course': course.id })}}"><i class="fas fa-plus me-1"></i>Bulletin</a> </td> </tr> {% endfor %} {% endfor %} </tbody> </table>
Rostand Ngagoum, il y a 4 ans

Un grand merci a vous les gars d'avoir pris le temps de me repondre et d'apporter une solution à mon problème