phpsymfonyknppaginatorbundle

Paginate results whit KipPaginatorBundle fail


I'm create form search like this :

when I run the search , it shows me the first page with 10 elements, but when I click on page 2 , it shows me all the elements of my database without considering my criteria search. I do not understand

class FrontController extends Controller
{
       /**
        * @Route("/", name="front_homepage")
        */
        public function indexAction(){

             $form = $this->createForm(SearchType::class);

             return $this->render('FrontBundle::layout.html.twig', array(
                 'form' => $form->createView(),
             ));
        }

}


class SearchController extends Controller
{
    /**
     * @param Request $request
     * @Route("/search/form", name="front_search_form")
     */
    public function searchOffreAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();

        $form = $this->createForm(SearchType::class);

        $form->handleRequest($request);

        if($form->isSubmitted()) {

           $entity = $em->getRepository('CoreBundle:Foo')->findByCritere($form->getData());

            /** @var Paginator $paginator */
            $paginator  = $this->get('knp_paginator');
            $result = $paginator->paginate(
                $entity,
                $request->query->getInt('page', 1),
                10
            );

            return $this->render('FrontBundle:Search:offre.html.twig', ['offres' => $result]);

        }

       return $this->createNotFoundException();
    }

}

thank you all,


Solution

  • I think the problem is your $entity needs to be a query not a result.

    Try this instead:

    $em = $this->getDoctrine()->getManager();
    $dql   = "SELECT f FROM CoreBundle:Foo f";
    $query = $em->createQuery($dql);
    
    /** @var Paginator $paginator */
    $paginator  = $this->get('knp_paginator');
    $result = $paginator->paginate(
         $query,
         $request->query->getInt('page', 1),
         10
    );
    

    Not sure if it will work, but try it.