0

我正在使用 KnpPaginatorBundle 并且我在 Repository 中使用 QueryBuilder 来选择数据。我不得不将参数sortdirection放在变量$sort$direction中,并将它们传递给 Query 以便能够执行和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
    ));
}

旅游资料库:

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();

}
4

2 回答 2

1

@phpisuber01 是对的,他的回答帮助我理解。不幸的是,KnpPaginator 官方网站没有给出 QueryBuilder 的示例,但在https://github.com/KnpLabs/KnpPaginatorBundle中确实提到了 /* query 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>

这就对了。

于 2015-12-20T16:22:23.293 回答
0

knp_paginator 本身对来自 url 的排序和方向关键字起作用,您不必传递它们。见文档 - https://github.com/KnpLabs/KnpPaginatorBundle

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

$pagination = $paginator->paginate()  

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

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

$paginator  = $this->container->get('knp_paginator');
于 2015-03-27T13:24:16.263 回答