attributs de jointures many_to_many
Bonjour a tous,
j 'ai une relation many to many Sujet - Tag , j 'ai recuperé les tags utilisés par mes sujets , je me bloque dans l 'affichage , je veux affiche aux sujet N1 ses tags associés.
J'ai implementé une solution-test et ca marche pour sujet id=1 et sujet id =2 pour le moment .
J 'ai besoin de vos propositions selon mon code pour que ça marche a tout les sujets de ma bdd.Merci
SujetCongtrolleur :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public function sujetrecentAction() {
$em = $this->getDoctrine()->getManager();
/* * ***** select all sujet from table ** */
$sujet = $em->getRepository('MyAppForumBundle:sujet')->getAllsujet();
/* * ***** select all tag from table association ** */
$tag1 = $em->getRepository('MyAppForumBundle:tag')->getBySujet(1);
$tag2 = $em->getRepository('MyAppForumBundle:tag')->getBySujet(2);
// var_dump($tag5);
// die();
return $this->render('MyAppForumBundle:sujet:sujetrecent.html.twig', array(
'sujet' => $sujet,
'tag2' => $tag2 , 'tag1' => $tag1
)
);
} |
tagRepository :
Code:
1 2 3 4 5 6
| public function getBySujet($id) {
return $this->getEntityManager()
->createQuery(' SELECT t FROM MyAppForumBundle:Tag t JOIN t.sujets s WHERE s.id=:id ')
->setParameter('id',$id)
->getResult();
} |
la vue :
Code:
1 2 3 4 5 6 7 8 9 10
|
{% for sujet in sujet %}
{{sujet.contenu|Contenu}}
{%if sujet.id ==1%} {% for tag1 in tag1 %} <b>{{tag1.title}}</b>{%endfor%} {%endif%}
{%if sujet.id == 2%} {% for tag2 in tag2 %} <b>{{tag2.title}}</b>{%endfor%} {%endif%}
{%endfor%} |