mike
mike

Reputation: 63

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();
    }

Upvotes: 0

Views: 4658

Answers (2)

mike
mike

Reputation: 63

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();

Upvotes: 1

leberknecht
leberknecht

Reputation: 1714

The problem is that you execute the query (its actually strange that the paginator doesnt complain about that...there should be an error like "Expecting type query, got ArrayCollection" or something like that.

The paginator expects that you pass a Query object, then the paginator will "extend" that query with setMaxResults and setFirstResult, depending on the parameters from the request.

When you fire findBy or your findByType, the result is an ArrayCollection, so at that point its already "too late" to paginate.

Maybe im missing something here, can you double check that the code that you provided is really "working, but not as-expected"?

Upvotes: 0

Related Questions