duanshang9426 2019-06-20 16:28
浏览 90

Symfony中的Api分页

I am working on API, I have a controller that query for results to a framework7 app. (first, last and both). I, however, want to add pagination from API to framework but I have not paginated in Symfony before, what is the best practice?

I tried paginator but there are not many examples of it. Below is my controller function.

 public function getEventSessionAttendeeAction(request $request, $eventSessionId)
{
    $searchFields = [
        'o.email',
        'a.email',
        'a.firstName',
        'a.lastName',
        'a.barcode1',
        'a.barcode2',
        'a.id',
        'o.id'
    ];

    /** @var \KCM\ApiBundle\Entity\Api\EventSession $eventSession */
    $eventSession = $this->get('doctrine')->getRepository('KCMApiBundle:EventSession')->findOneBy(
        [
            'id' => $eventSessionId
        ]
    );
    /** @var ApiEntity\Event $event */
    $event = $eventSession->getEvent();
    $childSafe = $event->getChildSafe();

    $filter = $request->get('filter');
    $match_level = 0;

    //Searches by email address
   if ( filter_var($filter, FILTER_VALIDATE_EMAIL)) {
    $searchFields = [
        'a.email'
    ];
    $match_level = 1;

    //Searches for first or last name
    }elseif(preg_match('/^[a-zA-Z\-]*$/', $filter)){
      $searchFields = [
         'a.lastName',
         'a.firstName'
     ];
      $match_level = 2;

    //Searches by barcode
   }elseif (preg_match('/^[0-9]+/', $filter)){

       if ($childSafe == 2) {
           $searchFields = [
               'a.barcode3'
           ];
       } else {
           $searchFields = [
               'a.barcode1'
           ];
       }
       $match_level = 4;
   }

    //Searches for first and last name(must have at least first characters for each
  if(preg_match('/^([a-z|A-Z]+)\s{1}([a-z|A-Z]+)/', $filter)){
       $match_level = 3;
   }elseif (preg_match('/^\s*$/', $filter)){
        $match_level = 5;
  }

    try {
      /** @var ApiEntity\EventSession $eventSession */

        $em = $this->getDoctrine()->getManager();
        $qb = $em->createQueryBuilder();
        $qb->select(array('a', 'o'))
            ->from('KCMApiBundle:EventAttendee', 'a')
            ->join('a.eventOrder', 'o')
            ->join('o.event', 'e')
            ->join('e.eventSessions', 'es')
            ->where($qb->expr()->andX(
               $qb->expr()->eq('es.id', $qb->expr()->literal($eventSessionId))
           ))
            ->andWhere('a.sessionCheckedIn = 1');

        if ($match_level === 3){
            $expr = $qb->expr()->andX();
            list($first, $last) = explode(' ', $filter);
                $last =  $last. '%';
                $expr->add($qb->expr()->like('a.lastName', $qb->expr()->literal($last)));
                $first =  $first. '%';
                $expr->add($qb->expr()->like('a.firstName',$qb->expr()->literal($first)));

        }elseif ($match_level === 5){
            $expr = $qb->expr()->andX();

            $paginator = new Paginator($qb);
            $paginator->getQuery()
                ->setFirstResult(0)
                ->setMaxResults(10);

        }else {
            $expr = $qb->expr()->orX();
            foreach ($searchFields as $field) {

                if ($match_level === 1) {
                    $literalFilter = $filter;
                    $expr->add($qb->expr()->like($field, $qb->expr()->literal($literalFilter)));
                } elseif ($match_level === 2) {
                    $literalFilter = $filter;
                    $expr->add($qb->expr()->like($field, $qb->expr()->literal($literalFilter)));
                } elseif ($match_level === 4) {
                    $literalFilter = $filter;
                    $expr->add($qb->expr()->eq($field, $qb->expr()->literal($literalFilter)));
                }
            }
        }

       $qb->andWhere($expr);
       $results = $qb->getQuery()->getResult();


        if ($results) {
            return $this->getApi()->serialize($results);
        }

        return new Response(null, Response::HTTP_NOT_FOUND);

    } catch (\Exception $e) {
        $this->get('logger')->error($e->getMessage());
    }
}

What I am trying to eventually do is to be able to use this query to paginate to the framework7 app. should I create a new public function to paginate or is there a way to do it within this function?

  • 写回答

1条回答 默认 最新

  • dqcj32855 2019-06-20 17:22
    关注

    Did you try to use knppagination bundle?

    https://github.com/KnpLabs/KnpPaginatorBundle

    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源