Count the number of records in my BD ManyToMany

0

I do not know how to explain to me so that they understand me, I even think that I asked the question badly, let's see, I need to know how (I better put examples), I have a reservation table, and a food table, their relationship is much To much, when for example I render the reservations that there are in the BD it is this way to me (the numbers are only the id):

That is, I have 3 reservations with those foods, what I want is like in my controller in Symfony, I say that what I need to render is:

That is, count how many requests of each food I have. Taking into account the relationships mentioned above, how would you do it? ... Thank you very much

    
asked by Daniel David Díaz González 05.06.2018 в 21:05
source

1 answer

0

I already solved, owww, Doctrine is a gift ajaj, well, in the Repository I did this:

  public function almacen($menu)
{
    $em = $this->getEntityManager();
    $consulta = $em->createQueryBuilder();
    $consulta
        ->select('a')
        ->from('AdminBundle:Alimento', 'a')
        ->innerJoin('a.reserva', 'r')
        ->where('r.menu = :menu')
        ->setParameter('menu', $menu);
    // ->orderBy('a.nombre', 'ASC');
    return $consulta->getQuery()->getResult();
}

On the controller:

 /**
 * Lista todos los alimentos que hay que consumir en el menu para solicitarlo en el almacen.
 * @Route("/list", name="admin_almacen_list", condition="request.isXmlHttpRequest()")
 * @Method("GET")
 */
public function listAction()
{
    $em = $this->getDoctrine()->getManager();
    $menu = $em->getRepository('AdminBundle:Menu')->findOneByFecha(new \DateTime('tomorrow'));
    $alimentos = $em->getRepository('AdminBundle:Reserva')->almacen($menu);
    return $this->render('AdminBundle:Almacen:list.html.twig', array(
        'alimentos' => $alimentos,
    ));
}

And in the view, super simplicity:

{% for a in alimentos %}
{{ a }}...{{ a.reserva.count }}<br>

{% endfor%}

It was not so complex, only that sometimes deconcentration wins ...

    
answered by 07.06.2018 в 18:37