Knp Paginator with findAll () method

I have a problem that paginator knp only works like this:

$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 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 that? I do not understand.

Here is my call:

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

Greetings Michael

+5
source share
4 answers

KNP does not support sorting array elements, as described here .

Improve database retrieval and sorting of data. In your second example, you extract all the data from the table (and maybe more), then you ask paginator for their restriction. This does not work well. Therefore, it is better to do this work with the request and allow you to bypass the paginator element.

Pnginator KNP can currently be paginated:

  • array
  • Doctrine\ORM\Query
  • Doctrine\ORM\QueryBuilder
  • Doctrine\ODM\MongoDB\Query\Query
  • Doctrine\ODM\MongoDB\Query\Builder
  • Doctrine\Common\Collection\ArrayCollection - any collection of doctrine relationships, including ModelCriteria - Propel ORM request
  • with Solarium_Client and Solarium_Query_Select as elements

See doc fererence for more details.

my two cents

+4
source

findAll() compatible with Knp_paginator, you just need to give it to your pointer:

 $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 ); 
+6
source

FindAll returns an array. To break a page into a member, a doctrine request object is required.

+3
source

You can try the following:

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

Source: https://habr.com/ru/post/1204011/


All Articles