Knp Pagination in symfony 5

2.5k Views Asked by At

I installed "Knp Paginator" and everything works fine but if i try to do a search in the search bar (this search bar does not depend on Knp Paginator) i have this error :

"Knp\Bundle\PaginatorBundle\Twig\Extension\PaginationExtension::render(): Argument #2 ($pagination) must be of type Knp\Bundle\PaginatorBundle\Pagination\SlidingPaginationInterface, array given, called in... /var/cache/dev/twig/36/36da62d992e743004744882a10f47b6d89340c107d735e823c151b7c459ca09f.php on line 214" =>see image.

If i disable Knp Paginator, the search bar work again.

Thank you for your help.

this is my queryBuilder of search bar (ItemRepository):

public function findBySearch($search) 
    {
        return $this->createQueryBuilder('a')
            ->andWhere('a.city LIKE :val')
            ->setParameter('val', '%' . $search . '%')
            ->orWhere('a.title LIKE :title')
            ->setParameter('title', '%' . $search . '%')
            ->orWhere('a.city LIKE :city')
            ->setParameter('city', '%' . $search . '%')
            ->orWhere('a.zipCode LIKE :zip')
            ->setParameter('zip', '%' . $search . '%')
            ->orWhere('a.hiddenDetail LIKE :hiddenDetail')
            ->setParameter('hiddenDetail', '%' . $search . '%')
            ->getQuery()
            ->getResult();
    }

Controller (searchBar) :

   /**
    *@Route("/searchItem", name="searchItem")
    *
    */
   public function searchItem(Request $request, ItemRepository $itemRepository)
   {

       $search = $request->request->get('search');
       $items = $itemRepository->findBySearch($search);


       return $this->render('home/listItem.html.twig', [
           
           'items' => $items
           

       ]);
   }

Controller (Knp Paginator) :

public function listItem(ItemRepository $repository, PaginatorInterface $paginator, Request $request): Response
    {

       
        $data = $repository->findAll();

        // PAGINANTION KNP/PAGINATOR
        $items = $paginator->paginate(
            $data,
            $request->query->getInt('page', 1),
            8

        );

        return $this->render('home/listItem.html.twig', [

            'items' => $items,

        ]);
    }
1

There are 1 best solutions below

0
On

KNP is receiving an array from your findAll() function. You need to write a custom repo function to return the query builder itself, and KNP will complete the query for you with any extra necessities.

see https://symfonycasts.com/screencast/symfony4-doctrine-relations/pagination

It was fruitful in my endeavors.

Edit: Also, I would take another look at your andWhere()'s. You can set one parameter for all of them at once, and just reuse the search term, if that would work for your situation.

public function findBySearch($search) 
    {
        return $this->createQueryBuilder('a')
            ->andWhere('
              a.city LIKE :val
              OR a.title LIKE :val
              OR a.city LIKE :val
              OR a.zipCode LIKE :val
              OR a.hiddenDetail LIKE :val')
            ->setParameter('val', '%'.$search.'%')
            ->getQuery()
            ->getResult();
            // if you want to use the paginator, leave these last two
            // lines off. Return just the qb.
    }