首页 > 解决方案 > symfony 5 获取多对多关系的数据

问题描述

我如何获得表的 user_id :user_evento

我需要比较谁用户加入表:evento

在此处输入图像描述

控制器

    public function evento_detalle(Evento $evento){
        if(!$evento){
          return $this->redirectToRout('ver_eventos');
        } 
        $salas = $this->getDoctrine()->getRepository(Sala::class)->findBy([],['id' => 'desc']);
        $users =  $this->getDoctrine()->getRepository(User::class) ->findBy([],['id' => 'ASC']);

        return $this->render('evento/ver-evento.html.twig',[
            'eventos' =>$evento,
            'users' =>$users,
            'salas' =>$salas
        ]);

    }

枝条

{% for user in users %}
    {% if user.id == eventos.getUsers.evento %}
    <tr>
        <td>

        {{ user.nombre }} 

        </td>
        <td> </td>
        <td> </td>
        <td> </td>
        <td>  </td>

    </tr>
    {%endif%}
{% endfor %}    

标签: symfony

解决方案


如果你试图列出Users与特定对象相关的列表,Evento你应该在方法返回的集合中找到它们Evento::getUsers(),前提是你遵循了 Symfony 的最佳实践。

{% for user in eventos.getUsers %}
  ...
  {{ user.nombre }} 
  ...
{% endfor %}    

推荐阅读