简体   繁体   English

使用 findAll() 方法的 Knp 分页器

[英]Knp Paginator with findAll() method

I have the Problem, that the knp paginator only works like this:我有问题,knp 分页器只能这样工作:

    $em    = $this->get('doctrine.orm.entity_manager');
    $dql   = "SELECT a FROM MainArtBundle:Art a";
    $query = $em->createQuery($dql);


    $paginator  = $this->get('knp_paginator');
    $pagination = $paginator->paginate(
        $query,
        $this->get('request')->query->get('page', 1)    /*page number*/,
        8                                        /*limit per page*/
    );

But not in this way:但不是这样:

    $em         = $this->getDoctrine()->getManager();
    $entities   = $em->getRepository('MainArtBundle:Art')->findAll();

    $paginator  = $this->get('knp_paginator');
    $pagination = $paginator->paginate(
        $entities,
        $this->get('request')->query->get('page', 1)    /*page number*/,
                                                      /*limit per page*/
    );

Why is it like this?为什么会这样? I don't understand.我不明白。

Here´s my twig call :这是我的树枝电话:

<li>{{ knp_pagination_sortable(paginator, 'Oldest', 'a.id', {'direction': 'desc'}) }}</li>

Greetings Michael问候迈克尔

findAll() is compatible with Knp_paginator, you just have to give it to your paginator : findAll()与 Knp_paginator 兼容,您只需将其提供给您的分页器:

 $query = $em->getRepository('Acme\\FOOBundle\\Entity\\BAR')->findAll(); $paginator = $this->get('knp_paginator'); requests = $paginator->paginate( $query, $this->get('request')->query->get('page', 1), 5 );

KNP don't support sorting of array elements, as described here . KNP不支持的数组元素的排序,如所描述这里

Better extract and sort data at database level.在数据库级别更好地提取和排序数据。 In your second example you fetch all data from table (and can be bigger), then you ask at the paginator to limit them.在您的第二个示例中,您从表中获取所有数据(并且可以更大),然后您要求分页器限制它们。 This don't perform well.这表现不佳。 So is better to do this work with a query and let do manage to the paginator element.所以最好使用查询来完成这项工作,并让我们管理分页器元素。

Currently KNP Paginator can paginate:目前 KNP Paginator 可以分页:

  • array
  • Doctrine\\ORM\\Query
  • Doctrine\\ORM\\QueryBuilder
  • Doctrine\\ODM\\MongoDB\\Query\\Query
  • Doctrine\\ODM\\MongoDB\\Query\\Builder
  • Doctrine\\Common\\Collection\\ArrayCollection - any doctrine relation collection including ModelCriteria - Propel ORM query Doctrine\\Common\\Collection\\ArrayCollection - 任何学说关系集合,包括ModelCriteria - Propel ORM 查询
  • array with Solarium_Client and Solarium_Query_Select as elementsSolarium_ClientSolarium_Query_Select作为元素的数组

See the doc fererence for detail有关详细信息,请参阅文档参考

my two cents我的两分钱

FindAll returns an array. FindAll 返回一个数组。 The knp paginator requires a doctrine query object. knp 分页器需要一个学说查询对象。

你可以试试这个:

$query = $repository->createQueryBuilder('a');

You can try this too :你也可以试试这个:

public function index(PaginatorInterface $paginator, Request $request): Response
{
    $properties = $paginator->paginate(
        $this->repository->findAllVisibleQuery(),
        $request->query->getInt('page', 1),
        10
     );
     return $this->render('pages/property.html.twig',[
         'current_menu'=> 'properties',
         'properties'=> $properties
         ]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM