Query a ManyToMany relation and display the good result in Symfony with Doctrine

后端 未结 2 2123
故里飘歌
故里飘歌 2020-12-11 09:08

in order to know how it really works, there is an unanswered question from Stack website, and notice that I have the similar problem.

In my SQl database, I

2条回答
  •  遥遥无期
    2020-12-11 09:45

    You shouldn't use DQL or others direct queries in your controllers if not really necessary. You should do this:

    public function indexAdvertsAction() {
        $em=$this->getDoctrine()->getManager();
        $adverts = $em->getRepository('MySpaceMyBundle:Adverts')->findAll();
    
        return $this->render(
             'MySpaceMyBundle:MyFolder:indexAdverts.html.twig',
             array('adverts' => $adverts )
        );
    }
    

    Then, in your template, the advert entity will take care of the rest, thanks to the correct relations mapping:

    {% for adverts in advert%}
       
           {{ adverts.id }}
           {{ adverts.name }}
           {{ adverts.users }}
           
               {% for category in adverts.categories %}
                   {{ adverts.categories }}
               {% endfor %}
           
           
               
           
       
    {% endfor %}
    

提交回复
热议问题