Slowwie
Slowwie

Reputation: 1246

Knp Paginator with findAll() method

I have the Problem, that the knp paginator 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 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

Upvotes: 6

Views: 8232

Answers (5)

Add
Add

Reputation: 19

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
         ]);
}

Upvotes: 0

jmunozco
jmunozco

Reputation: 695

You can try this:

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

Upvotes: 0

GregOs
GregOs

Reputation: 413

findAll() is compatible with Knp_paginator, you just have to give it to your 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
);

Upvotes: 8

Matteo
Matteo

Reputation: 39390

KNP don't support sorting of array elements, as described here.

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:

  • 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
  • array with Solarium_Client and Solarium_Query_Select as elements

See the doc fererence for detail

my two cents

Upvotes: 5

Derick F
Derick F

Reputation: 2769

FindAll returns an array. The knp paginator requires a doctrine query object.

Upvotes: 3

Related Questions