I'm currently working on a Symfony project where I need to render a list of "mededelingens" (announcements) based on the user's role. I have two routes, one for "leerling" (student) and one for "instructeur" (instructor), and I'm using the findBy method to retrieve the appropriate data from the MededelingenRepository.
However, I'm encountering an issue where the loop in the Twig template does not display any data. I suspect there might be an error in the code related to passing the "rol" parameter to the findBy method.
/**
* @Route("/leerling/mededelingen", name="app_leerling")
*/
public function show(MededelingenRepository $mededelingenRepository) {
$mededelingen = $mededelingenRepository->findBy(["rol" => '["ROLE_LEERLING"]']);
return $this->render('leerling/mededelingen.html.twig',
['mededelingen' => $mededelingen]
);
}
/**
* @Route("/instructeur/mededelingen", name="app_instructeur")
*/
public function show(MededelingenRepository $mededelingenRepository) {
$mededelingen = $mededelingenRepository->findBy(["rol" => '["ROLE_INSTRUCTEUR"]']);
return $this->render('instructeur/mededelingen.html.twig',
['mededelingen' => $mededelingen]
);
}
{% for mededelingens in mededelingen %}
<p>{{ mededelingens.name }}</p>
{% endfor %}
$builder->add('rol', ChoiceType::class, [
'choices' => [
'["ROLE_LEERLING"]' => ["ROLE_LEERLING"],
'["ROLE_INSTRUCTEUR"]' => ["ROLE_INSTRUCTEUR"],
],
]);
Your code has been rewritten .
/**
* @Route("/leerling/mededelingen", name="app_leerling")
*/ public function show(MededelingenRepository $mededelingenRepository) {
$mededelingen = $mededelingenRepository->findBy(["rol" => '["ROLE_LEERLING"]']);
return $this->render('leerling/mededelingen.html.twig',
[ 'mededelingen' => $mededelingen,
]);
}
/**
* @Route("/instructeur/mededelingen", name="app_instructeur")
*/ public function show(MededelingenRepository $mededelingenRepository) {
$mededelingen = $mededelingenRepository->findBy(["rol" => '["ROLE_INSTRUCTEUR"]']);
return $this->render('instructeur/mededelingen.html.twig',
[ 'mededelingen' => $mededelingen,
]);
}
{% for mededelingens in mededelingen %}
<p> {{ mededelingens.name }} </p>
{% endfor %}
$builder->add('rol', ChoiceType::class, [
'choices' => [
'["ROLE_LEERLING"]' => ["ROLE_LEERLING"],
'["ROLE_INSTRUCTEUR"]' => ["ROLE_INSTRUCTEUR"],
],
]);