如何将 KnpPaginator 手动计数移动到存储库类



我正在使用 knppaginator 对非常大的实体集合进行分页,在对性能不满意之后,我意识到无论传递什么页码,它们都会获取所有实体,我检查了查询探查器,但我从未在查询中看到任何偏移量。无论如何,这就是我发现有效的方法,但我想将获取计数和实体的部分移回存储库类,在那里我想使用 createQueryBuilder

public function indexAction(Request $request)
{
    $page = $request->get('p', 1);
    $this->breadcrumbs(
        array(
            array('title'=>'Admin','path'=>'home'),
            array('title'=>'Customers','path'=>'customers'),
            array('title'=>'All'),
        )
    );
    $em = $this->getDoctrine()->getManager();

    $count = $em
        ->createQuery('SELECT COUNT(cb) FROM MWCoreBundle:ContactsBasics cb LEFT JOIN cb.login l LEFT JOIN cb.setting s')
        ->getSingleScalarResult()
    ;
    $entities = $em
        ->createQuery('SELECT cb,l,s FROM MWCoreBundle:ContactsBasics cb LEFT JOIN cb.login l LEFT JOIN cb.setting s ORDER BY cb.dateAdded DESC')
        ->setHint('knp_paginator.count', $count);

    $pagination = $this->get('knp_paginator')->paginate($entities, $page, 50, array('distinct' => false));

    return $this->render('MWCoreBundle:ContactsBasics:index.html.twig', array(
        'entities' => $entities,
        'pagination' => $pagination,
    ));
}
no,

knp paginator, 运行三个查询:

  1. 计数项目

  2. 通过偏移量获取不同的 ID

  3. 按 ID 获取项目

计数项目:

SELECT 
  count(DISTINCT u0_.id) AS sclr_0 
FROM 
  users u0

通过偏移量获取不同的 ID

SELECT 
    DISTINCT u0_.id AS id_0 
FROM 
  users u0_ 
LIMIT 
  10 OFFSET 0

按 ID 获取项目:

SELECT 
    *
FROM 
  users u0_ 
WHERE 
  u0_.id IN (?)

相关内容

  • 没有找到相关文章

最新更新