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

Matteo
  • 37,680
  • 11
  • 100
  • 115
Slowwie
  • 1,146
  • 2
  • 20
  • 36
  • 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)` – blamb May 27 '15 at 21:32
  • `in vendor/knplabs/knp-paginator-bundle/Twig/Extension/PaginationExtension.php at line 56 ` `$template ?: $pagination->getTemplate(),` – blamb May 27 '15 at 21:36

5 Answers5

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
);
jmattheis
  • 10,494
  • 11
  • 46
  • 58
GregOs
  • 403
  • 3
  • 13
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

Matteo
  • 37,680
  • 11
  • 100
  • 115
  • Is it also possible to paginate a simple string ? – Slowwie Oct 06 '14 at 14:12
  • 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? – Matteo Oct 06 '14 at 14:20
  • 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. – Slowwie Oct 06 '14 at 14:28
  • Doesn't knp paginator even support a normal array? I fetch my data from a stored procedure which returns then an array. – Adrian Nov 13 '15 at 10:42
  • 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) – Matteo Nov 13 '15 at 12:42
  • Somehow my code doesn't work, it doesn't sort it at all. A table head looks like this `{{ knp_pagination_sortable(tasks, 'ID', 'ID') }}` @Matteo 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'`? – Adrian Nov 13 '15 at 12:49
  • @adiii4 as mentioned in my answer knp don't sort array, you can take a look at [this implementation](https://github.com/KnpLabs/knp-components/pull/129). Hope this help – Matteo Nov 13 '15 at 13:27
  • @adiii4 if you are passing a query to the paginate method, yes: you need to set the table alias also. – Matteo Nov 13 '15 at 13:31
3

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

Derick F
  • 2,749
  • 3
  • 20
  • 30
  • Also it is not possible to do it with the simple findAll() method ? – Slowwie Oct 04 '14 at 20:58
  • 1
    the short answer is no you can't. you need to make your own repository method. – Derick F Oct 04 '14 at 21:31
  • Currently paginator can paginate various type, see the [doc fererence](https://github.com/KnpLabs/KnpPaginatorBundle#controller) for detail – Matteo Oct 05 '14 at 11:34
  • Doesn't knp paginator even support a normal array? I fetch my data from a stored procedure which returns then an array. – Adrian Nov 13 '15 at 12:00
0

You can try this:

$query = $repository->createQueryBuilder('a');
jmunozco
  • 585
  • 2
  • 11
  • 24
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
         ]);
}
Add
  • 19
  • 5