Symfony2,使用带有KnpPaginatorBundle的QueryBuilder

时间:2014-11-26 19:09:13

标签: php symfony doctrine-orm knpmenubundle

我正在使用KnpPaginatorBundle,我在Repository中使用QueryBuilder来选择数据。我不得不在变量 $ sort $ direction 中添加参数排序方向,并将它们传递给查询能够做到和 orderBy

此代码有效,但我想向您展示并表达您的意见,如果使用KnpPaginatorBundle这是一个很好的方法,或者knpPaginatorBundle提供了另一种更好的方法。

我可以直接将这些参数传递给不在queryBuilder中的knp_paginator吗?

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 个答案:

答案 0 :(得分:1)

@ phpisuber01是对的,他的回答有助于我理解。不幸的是,官方的KnpPaginator网站没有给出QueryBuilder的例子,但在https://github.com/KnpLabs/KnpPaginatorBundle中提到/ *查询NOT result * /。

以下是QueryBuilder的示例代码:

存储库中的第一个:

/*
 * 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();
}

控制器中的第二名:

 /*
  * 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));
}

观点中的第三个:

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

就是这样。

答案 1 :(得分:0)

knp_paginator,它自己处理来自url的sort和direction关键字,你不必传递它们。 见doc - https://github.com/KnpLabs/KnpPaginatorBundle

$ listTravels,它将返回数据库中的所有结果 - 如果您有太多记录,该怎么办。

$pagination = $paginator->paginate()  

仅根据您的限制获取有限的结果。

您可以在将$paginator = $this->get('knp_paginator');作为参数或任何其他方法传递后直接在存储库类中使用$this->container并可以使用

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