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

后端 未结 2 2124
故里飘歌
故里飘歌 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%}
       <tr>
           <td>{{ adverts.id }}</td>
           <td>{{ adverts.name }}</td>
           <td>{{ adverts.users }}</td>
           <td>
               {% for category in adverts.categories %}
                   {{ adverts.categories }}
               {% endfor %}
           </td>
           <td>
               <a href="{{ path('editAdverts', {'name': adverts.name}) }}"><button class="btn btn-warning btn-xs">Edit</button></a>
           </td>
       </tr>
    {% endfor %}
    
    0 讨论(0)
  • 2020-12-11 09:50
    WHERE a.categories = c.id
    

    This line should cause the error. I think in your case, you should use a.categories.id instead of a.categories. You can't equalize an object with an integer.

    0 讨论(0)
提交回复
热议问题