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条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化