As I already wrote , I am doing a test task on symfony 3.

In Laravel's query builder, there is a paginate() method that allows you to quickly and easily make pagination output.

I can not find an analogue in symfony 3.

Is there no such necessary thing?

    3 answers 3

    Let's clarify, you are using Doctrine ORM, for pagination there is one solution:

     use Doctrine\ORM\Tools\Pagination\Paginator; $dql = "SELECT p, c FROM BlogPost p JOIN p.comments c"; $query = $entityManager->createQuery($dql) ->setFirstResult(0) ->setMaxResults(100); $paginator = new Paginator($query, $fetchJoinCollection = true); $c = count($paginator); foreach ($paginator as $post) { echo $post->getHeadline() . "\n"; } 

    taken from here

      I prefer KnpPaginatorBundle

      STEP 1: Install using composer

       composer require knplabs/knp-paginator-bundle 

      STEP 2: Initialization in the kernel

       public function registerBundles() { return array( // Предыдущий код new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(), // Последующий код ); } 

      STEP 3: Add to config.yml

       knp_paginator: page_range: 5 # default page range used in pagination control default_options: page_name: page # page query parameter name sort_field_name: sort # sort field query parameter name sort_direction_name: direction # sort direction query parameter name distinct: true # ensure distinct results, useful when ORM queries are using GROUP BY statements template: pagination: 'KnpPaginatorBundle:Pagination:twitter_bootstrap_v3_pagination.html.twig' 

      STEP 4: Make a request to the database in the Controller and pass it to the paginator

       public function listAction(Request $request) { $em = $this->get('doctrine.orm.entity_manager'); $dql = "SELECT a FROM AcmeMainBundle:Article a"; $query = $em->createQuery($dql); $paginator = $this->get('knp_paginator'); $pagination = $paginator->paginate( $query, /* query NOT result */ $request->query->getInt('page', 1)/*page number*/, 10/*limit per page*/ ); // parameters to template return $this->render('AcmeMainBundle:Article:list.html.twig', ['pagination' => $pagination]); } 

      STEP 5: Insert the paginator in the template in the required place

       <div class="navigation"> {{ knp_pagination_render(pagination) }} </div> 

      STEP 6: Enjoying the result

        You can use the KnpPaginatorBundle .

        Easy to use and configure, you can also customize as you like, documentation is also present .

        • Please try to publish detailed answers containing a specific example of the minimum solution, supplementing them with a link to the source. Answers –references (as well as comments) do not add knowledge to the Runet. - Nicolas Chabanovsky