Knp Paginator with findAll() method
Asked Answered
A

5

6

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

Audacity answered 4/10, 2014 at 16:18 Comment(2)
im having the same problem pretty much. i cant paginate off my entities directly, without building some kind of new query. i mean i already have my queries and entity usage how i like it, i just wanted to add paginate to it, and the documention only shows how to do it one way.. as your question demonstrates.. oddly in my case its trickling up to this error, which is very mis leading.. I want to add it to this topic for search ability Attempted to call method "getTemplate" on class "Symfony\Component\HttpFoundation\JsonResponse". (500 Internal Server Error)Cosmetic
in vendor/knplabs/knp-paginator-bundle/Twig/Extension/PaginationExtension.php at line 56 $template ?: $pagination->getTemplate(),Cosmetic
B
5

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

Burge answered 5/10, 2014 at 11:33 Comment(8)
Is it also possible to paginate a simple string ?Audacity
I add the documentation reference about what you can paginate. IF you have an array of string, yes: you can paginate it. Can you give more info about your problem?Burge
Also it is not possible to simply paginate a long string, like a book ? And what is about ajax ? How is the most simple workflow for an ajax paginated site with an doctrine query call? I don't want to have a full tutorial, only how to approach ... thanks.Audacity
Doesn't knp paginator even support a normal array? I fetch my data from a stored procedure which returns then an array.Caliphate
Hi @adiii4 yes of course, is the first of the list. You can have problem in dynamic sorting based on knp features (knp_pagination_sortable)Burge
Somehow my code doesn't work, it doesn't sort it at all. A table head looks like this <th>{{ knp_pagination_sortable(tasks, 'ID', 'ID') }}</th> @Burge is my second 'ID' wrong? What kind of syntax do I need, I can't find anything in the docs only for the querybuilder where the third parameter would be like this 't.ID'?Caliphate
@adiii4 as mentioned in my answer knp don't sort array, you can take a look at this implementation. Hope this helpBurge
@adiii4 if you are passing a query to the paginate method, yes: you need to set the table alias also.Burge
K
8

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
);
Kylstra answered 8/9, 2015 at 9:43 Comment(0)
B
5

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

Burge answered 5/10, 2014 at 11:33 Comment(8)
Is it also possible to paginate a simple string ?Audacity
I add the documentation reference about what you can paginate. IF you have an array of string, yes: you can paginate it. Can you give more info about your problem?Burge
Also it is not possible to simply paginate a long string, like a book ? And what is about ajax ? How is the most simple workflow for an ajax paginated site with an doctrine query call? I don't want to have a full tutorial, only how to approach ... thanks.Audacity
Doesn't knp paginator even support a normal array? I fetch my data from a stored procedure which returns then an array.Caliphate
Hi @adiii4 yes of course, is the first of the list. You can have problem in dynamic sorting based on knp features (knp_pagination_sortable)Burge
Somehow my code doesn't work, it doesn't sort it at all. A table head looks like this <th>{{ knp_pagination_sortable(tasks, 'ID', 'ID') }}</th> @Burge is my second 'ID' wrong? What kind of syntax do I need, I can't find anything in the docs only for the querybuilder where the third parameter would be like this 't.ID'?Caliphate
@adiii4 as mentioned in my answer knp don't sort array, you can take a look at this implementation. Hope this helpBurge
@adiii4 if you are passing a query to the paginate method, yes: you need to set the table alias also.Burge
M
3

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

Mono answered 4/10, 2014 at 17:31 Comment(4)
Also it is not possible to do it with the simple findAll() method ?Audacity
the short answer is no you can't. you need to make your own repository method.Mono
Currently paginator can paginate various type, see the doc fererence for detailBurge
Doesn't knp paginator even support a normal array? I fetch my data from a stored procedure which returns then an array.Caliphate
Z
0

You can try this:

$query = $repository->createQueryBuilder('a');
Zooplankton answered 13/7, 2016 at 17:3 Comment(0)
F
0

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
         ]);
}
Fundamental answered 16/12, 2020 at 21:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.