dezull/dbal-paginator-service-provider

一个基于 Silex 的 Doctrine DBAL 分页器提供程序,基于 knp pager 组件和 knp-paginator-bundle。

dev-master 2013-10-11 03:30 UTC

This package is not auto-updated.

Last update: 2024-09-23 15:20:39 UTC


README

基于 KnpLabs 的 PaginatorBundle

安装

添加到 composer.json

{
    "require": {
        "dezull/dbal-paginator-service-provider": "dev-master"
    }
}

用法

  1. 当注册 TwigServiceProvider 时,添加分页器的模板路径(创建自己的或直接使用 KnpPaginatorBundle 的)。
$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path' => array(
        /* Your other templates */
        __DIR__.'/../vendor/knplabs/knp-paginator-bundle/Knp/Bundle/PaginatorBundle/Resources/views',
    ),
));
  1. 注册 DBALPaginatorServiceProvider
$app->register(new Dezull\Silex\Provider\DBALPaginatorServiceProvider\DBALPaginatorServiceProvider(), array(
    /* The following assumes you use the template path as in step #1 */
    'dezull.dbal_paginator.template.pagination' => 'Pagination/twitter_bootstrap_pagination.html.twig',
    'dezull.dbal_paginator.template.sortable' => 'Pagination/sortable_link.html.twig',
));
  1. 在你的控制器中
public function indexAction(Request $request, Application $app)
{
    $page = (int) $request->query->get('page', 1);
    $sortKey = $request->query->get('sort', 's.id');
    $direction = $request->query->get('direction', 'desc');

    /* Doctrine DBAL QueryBuilder */
    $qb = $app['db']->createQueryBuilder()
        ->select('s.*')
        ->from('sometable', 's')
        ->orderBy($sortKey, $direction);

    $pagination = $app['dezull.dbal_paginator']->paginate(
        $qb,
        $page,
        20 /* per page limit */
    );

    return $app['twig']->render('Foo/index.html.twig', array(
        'pagination' => $pagination,
    ));
}
  1. 在模板中渲染分页
{{ dezull_dbal_pagination_render(pagination) }}

待办事项

  1. 移除对 KnpPaginatorBundle 的依赖