doumen6532 2014-11-26 19:09
浏览 31
已采纳

Symfony2,使用带有KnpPaginatorBundle的QueryBuilder

I'm using KnpPaginatorBundle and I'm using QueryBuilder in Repository to select data. I was obliged to put parameters sort and direction in variables $sort and $direction and pass them to the Query to be able to do and orderBy.

This code works but I'd like to show it to you and and have your opinions if it's a good way to do this with KnpPaginatorBundle or there is another better way provided by knpPaginatorBundle.

Can I pass those parameters directly to knp_paginator not in the queryBuilder ?

Action

public function listAction($page, Request $request)
{
    $em = $this->getDoctrine()->getManager();

    $paginator  = $this->get('knp_paginator');


    if($request->query->get('sort') and $request->query->get('direction')) {
        $sort = $request->query->get('sort') ;
        $direction = $request->query->get('direction');
    }
    else{
        $sort = 'id'; // set default sort
        $direction = 'asc';  // set default direction
    }   

    // pass those two variables to query
    $listTravels = $em->getRepository('ProjectTravelBundle:Travel')->getListTravelsFrontend($sort, $direction);

    $pagination = $paginator->paginate(
        $listTravels,
        $this->get('request')->query->get('page', $page)/*page number*/,
        10/*limit per page*/
    );

    return $this->render('ProjectFrontendBundle:Frontend:list.html.twig',array(
        'pagination' => $pagination
    ));
}

TravelRepository:

public function getListTravelsFrontend($sort, $direction) // parameters $sort, $direction
{

    $qb = $this->createQueryBuilder('t')
        ->leftJoin('t.image', 'i')
        ->addSelect('i')
        ->leftJoin('t.agence', 'a')
        ->addSelect('a')
        ->Where('t.enabled = 1')
        ->orderBy('t.'.$sort, $direction); // orderBy with those parameters

    return $qb->getQuery()->getResult();

}
  • 写回答

2条回答 默认 最新

  • doutangdan3588 2015-12-20 16:22
    关注

    @phpisuber01 is right, his answer helps me understand. Unfortunately, official KnpPaginator site does not give example for QueryBuilder but does mention /* query NOT result */ in https://github.com/KnpLabs/KnpPaginatorBundle.

    Here is the example code for QueryBuilder:

    First in Repository:

    /*
     * GiftDataRepository.php (in Repository/... directory)
     * return all records in table GiftData.
     */
    public function getLatestRecords()
    {
        $qb = $this->createQueryBuilder('q')->select('q');
        // getQuery() returns query NOT result.
        return $qb->getQuery();
    }
    

    Second in Controller:

     /*
      * TabulaRepository.php (in Controller/... directory)
      * return pagination object within template.
      */
     public function giftsDataShowAction(Request $request, $format)
    {
        $em             = $this->getDoctrine()->getManager();
        $giftsdataquery = $em->getRepository('AppBundle:GiftData')->getLatestRecords();
    
        $paginator = $this->get('knp_paginator');
        /* page number 1, limit per page 5 */
        $pagination = $paginator->paginate($giftsdataquery, $request->query->getInt('page', 1), 5);
    
        // parameters to template
        return $this->render('data/data_gifts.html.twig', array('pagination' => $pagination));
    }
    

    Third in View:

    <div class="count">Total of: {{ pagination.getTotalItemCount }}</div>
    <table class="table table-responsive table-striped">
        <thead>
        <tr>
            {# sorting of properties based on query components #}
            <th class="hidden-xs">{{ knp_pagination_sortable(pagination, 'Id', 'q.id') }}</th>
            <th {% if pagination.isSorted('q.circumstances') %} class="sorted"{% endif %}>{{ knp_pagination_sortable(pagination, 'Circumstances', 'q.circumstances') }}</th>
            <th class="hidden-xs">{{ knp_pagination_sortable(pagination, 'Date Of Report', 'q.dateOfReport') }}</th>
        </tr>
        </thead>
        <tbody>
    
        {% for row in pagination %}
            <tr {% if loop.index is odd %}class="color"{% endif %}>
                <td id="col" class="hidden-xs">{{ row.id }}</td>
                <td id="col">{{ row.circumstances }}</td>
                <td id="col" class="hidden-xs">{{ row.dateOfReport }}</td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
    

    That is it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度