我需要使用 jQuery DataTables 管理来自 Doctrine Paginator 查询的大量记录的显示。现在,我正在寻找完成此任务的最佳方法。
In terms of performance, it is better to paginate the results at database level, so you can avoid the overhead of mapping the results to objects, decrease the network traffic and your database server wont need to read/filter that many records [...] @Xocoatzin on this comment
我编写了一个 Symfony Controller 操作来从数据库检索帐户的数据:
AccountController
/**
* Lists all Account entities.
*
* @Route("/list/{page}", defaults={"page" = 1}, name="account_index")
* @Template("account/index.html.twig")
* @Method("GET")
*/
public function indexAction($page)
{
$limit = 15;
$offset = ($page - 1) * $limit;
$accountRepo = $this->getDoctrine()->getRepository('SupportBundle:Account');
$query = $accountRepo->createQueryBuilder('a')
->setFirstResult($offset)
->setMaxResults($limit)
->getQuery();
$paginator = new Paginator($query);
dump((int )$paginator->count());
$total = (int) ceil($paginator->count() / $limit);
dump($total);
$accounts = $query->getResult();
return array(
'accounts' => $accounts,
'title' => 'Accounts list',
'paginator' => $paginator->getIterator(),
'pages' => $total,
'current_page' => $page
);
}
为了渲染 View ,我实现了一个简单的 Twig 模板,其中包含填充我的表格的 for 。
我还发现了this gist用于 Symfony2/DataTables 集成。
我正在使用:
- Symfony 2.8.2
- jQuery 1.12
- 数据表 1.10.8
我发现了 Symfony 的几个分页器 bundle ,但我的目的是仅使用 Doctrine 分页器从头开始维护一种简单的方法,这样我将来就能够在不依赖第三方组件的情况下进行更改。我认为对于兼容性和解耦来说也会更好。
Paginator Symfony 组件:
最佳答案
关于jquery - 如何正确使用 jQuery DataTables 和 Doctrine 分页器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35450746/