使用 filter knp paginator 进行分页

pagination with filter knp paginator

我正在尝试借助分页进行过滤。当我尝试这样做时,第一页没问题,当我单击进入第二页时,它显示初始结果,因此没有任何过滤器。它们都放置在过滤器和显示器的同一路径中 这是我的控制器:

/**
     * @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,

            ]);
        }

这里是存储库中的查询:

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

问题是你执行了查询(分页器没有抱怨这真的很奇怪......应该有像 "Expecting type query, got ArrayCollection" 或类似的错误。

分页器期望您传递一个 Query 对象,然后分页器将 "extend" 使用 setMaxResultssetFirstResult 进行查询,具体取决于请求中的参数.

当您触发 findBy 或您的 findByType 时,结果是 ArrayCollection,因此此时已经 "too late" 进行分页。

也许我在这里遗漏了一些东西,你能仔细检查一下你提供的代码是否真的 "working, but not as-expected"?

我自己找到了解决方案。 ->setMethod('GET') 有所作为。

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