symfonypaginationknppaginator

pagination with filter knp paginator


I am trying to filter with the help of pagination. when I try and do that the first page is okay when I click to go in the second page it shows the initial result so without any filter. they are both placed in the same route the filter and the display this is my controller:

/**
     * @Route("/", name="home")
     */
    public function home(Request $request)
        {
            $property = new Property();

            $searchForm = $this->createFormBuilder($property)
                ->add('type', EntityType::class, [
                    'class' => Type::class,
                    'choice_label' => 'name',
                    'mapped' => false,
                    'expanded' => true,
                    'multiple' => true,
                    'label' => false,
                ])
                ->add('filter', SubmitType::class, [
                    'attr' => [
                        'class' => 'btn btn-outline-dark btn-rounded waves-effect'
                    ]
                ])
                ->getForm();

            $searchForm -> handleRequest($request);

            if ($searchForm->isSubmitted() && $searchForm->isValid()){
                $type = $searchForm->get('type')->getData();

                $search = $this->getDoctrine()->getRepository(Property::class)->findByType($type);
                if (count($type) == 0) {
                    $search = $this->getDoctrine()->getRepository(Property::class)->findBy(['isSold' => 0]);
                }

            } else {
                $search = $this->getDoctrine()->getRepository(Property::class)->findBy(['isSold' => 0]);
            }

            $paginator = $this->get('knp_paginator');
            $result = $paginator->paginate(
                $search,
                $request->query->getInt('page', 1),
                $request->query->getInt('limit', 10)
            );

            }

            return $this->render('home/index.html.twig', [
                'property' => $result,
                'searchForm' => $searchForm->createView(),
                'propertyCountByType' => $propertyCountByType,

            ]);
        }

here is the query in the repository:

public function findByType($type){
        $query = $this->createQueryBuilder('p')
            ->andWhere('p.type IN (:type)')
            ->andWhere('p.isSold = 0')
            ->setParameter('type', $type)
            ->getQuery();
        return $query->execute();
    }

Solution

  • I found the solution on my own . ->setMethod('GET') made the difference.

    $searchForm = $this->createFormBuilder($property)
                ->add('type', EntityType::class, [
                    'class' => Type::class,
                    'choice_label' => 'name',
                    'mapped' => false,
                    'expanded' => true,
                    'multiple' => true,
                    'label' => false,
                ])
                ->add('filter', SubmitType::class, [
                    'attr' => [
                        'class' => 'btn btn-outline-dark btn-rounded waves-effect'
                    ]
                ])
                ->setMethod('GET')
                ->getForm();