提问者:小点点

在symfony原则中,如何用group by在多个实例中恢复单个实例?


这里我有一个预约表,里面包括医生和病人。。。我想要的是每个医生的预约次数。 然而,在我的桌子上,我有两个医生有几个预约。 因此,我希望我的教义请求返回我的两个医生的名单,以及他们的预约总数,但相反,它将返回我的所有预约实例。

我在phpMyAdmin中的sql请求
这是我想要的结果:

我的控制器

public function bookingbydocAction(Request $request){
    $user = $this->getUser();
    if ($user === null) {
        throw new NotFoundHttpException('Utilisateur Inexistant');
    } else {
        $em = $this->getDoctrine()->getManager();
        $medecin = $em->getRepository('DoctixMedecinBundle:Medecin')->findAll();
        $bookings = $em->getRepository('DoctixFrontBundle:Booking')->findBy(array('medecin' => $medecin));
  $nbbookhonored = $em->createQuery('SELECT COUNT(b.id) AS nbr_booking_honored FROM 
  DoctixFrontBundle:Booking b WHERE b.statut like :approved GROUP BY b.medecin' )- 
   >setParameter('approved', 'approved')->getResult();

         $nbbookbydoc = $em->createQuery('SELECT COUNT(b.id) AS nbr_booking_by_doc FROM 
   DoctixFrontBundle:Booking b GROUP BY b.medecin' )->getResult();
   $nbbookhonored = $nbbookhonored[0]['nbr_booking_honored'];
        $nbbookbydoc = $nbbookbydoc[0]['nbr_booking_by_doc'];

        return $this->render('DoctixAdminBundle:User:booking_medecin.html.twig', array(
            'bookings' => $bookings,
            'nbbookhonored' => $nbbookhonored,
            'nbbookbydoc' => $nbbookbydoc

        ));
    }
}

我的观景小枝

<div class="table-responsive-sm">

                        <table class="table table-sm table-bordered" id="dataTable" width="100%" cellspacing="0">

                            <thead>
                            <tr>
                                <th>#</th>
                                <th>NOM COMPLET</th>
                                <th>EMAIL</th>
                                <th>TÉLÉPHONE</th>
                                <th>NOMBRE RENDEZ-VOUS</th>
                                <th>HONORES</th>
                            </tr>
                            </thead>
                            <tfoot>
                            <tr>
                                 <th>#</th>
                                <th>NOM COMPLET</th>
                                <th>EMAIL</th>
                                <th>TÉLÉPHONE</th>
                                <th>NOMBRE RENDEZ-VOUS</th>
                                <th>HONORES</th>
                            </tr>
                            </tfoot>
                            <tbody>

                            {% for booking in bookings %} 
                                <tr>
                                    <td>{{ loop.index }}</td> 
                                    <td>{{ booking.medecin.user.prenom|capitalize }} {{ booking.medecin.user.nom|upper }}</td>
                                    <td>
                                        {{ booking.medecin.user.username }}
                                    </td>
                                    <td>{{ booking.medecin.user.numTel }}</td>

                                    <td>{{ nbbookbydoc }}</td>
                                    <td>
                                        {{ nbbookhonored }}
                                    </td>
                                </tr>
                             {% endfor %}  
                            <div class="navigation">  </div>

                            </tbody>
                        </table>
                    </div>

问题是,它给我的视图中带来了两位医生的所有约会,或者我希望他给我带来每个医生的单个实例,以及他们各自约会的总次数。

存储库

public function findByBooking(){
    $qb = $this->createQueryBuilder('d')
    ->select('d.id, m.nom, m.prenom, m.username, m.numTel, COUNT(b.id) AS bookingsCount')
    ->join('d.bookings', 'b')
    ->join('d.user', 'm')
    ->groupBy('d.id');
    return $qb->getQuery()->getResult();
}

谢谢


共1个答案

匿名用户

作为理论的最佳实践,您应该使用存储库:

https://symfony.com/doc/4.4/docrine.html

您应该将'like'更改为'='以进行优化。 您没有使用任何通配符运算符(%)。

主要问题是您的查询。 您只需使用查询生成器将表连接在一起,然后对除Count之外的所有列使用group by。 只需确保您分组依据的所有列也在您的选择中。

编辑记住,我不知道您的实体结构,但应该是这样的:

$qb = $this->createQueryBuilder('booking');

return $qb->select('count(booking.id) as nbr_booking_by_doc, medecin.id, medecin.name')
    ->innerJoin('booking.medecin', 'medecin')
    ->innerJoin('booking.patient')
    ->where('booking.statut = :statut')
    ->groupBy('medecin.id')
    ->addGroupBy('medecin.name')
    ->setParameter('statut', 'approved')
    ->getQuery()
    ->getResult();