gpt4 book ai didi

symfony - 如何将 KnpPaginator 手动计数移动到 Repository 类?

转载 作者:行者123 更新时间:2023-12-02 03:27:12 25 4
gpt4 key购买 nike

我正在使用 knppaginator 对非常大的实体集合进行分页,在对性能不满意之后我意识到它们会获取所有实体,而不管是哪个页面number 正在传递,我检查了查询分析器,但我从未在查询中看到任何偏移量。无论如何,这是我发现可行的方法,但我想将获取计数和实体的部分移回存储库类,我想在其中使用 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,
));
}

最佳答案

不,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 (?)

关于symfony - 如何将 KnpPaginator 手动计数移动到 Repository 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29724180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com