Symfony 使用过滤器knp分页器分页

Symfony 使用过滤器knp分页器分页,symfony,pagination,knppaginator,Symfony,Pagination,Knppaginator,我正试图借助分页进行过滤。当我尝试这样做,第一页是好的,当我点击进入第二页,它显示了初始结果,所以没有任何过滤器。它们都放置在过滤器和显示器的同一路线上 这是我的控制器: /** * @Route("/", name="home") */ public function home(Request $request) { $property = new Property(); $searchForm =

我正试图借助分页进行过滤。当我尝试这样做,第一页是好的,当我点击进入第二页,它显示了初始结果,所以没有任何过滤器。它们都放置在过滤器和显示器的同一路线上 这是我的控制器:

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

问题是,您执行查询实际上很奇怪,paginator没有抱怨…应该有一个错误,如预期类型查询、Get ArrayCollection或类似的错误

paginator希望您传递一个查询对象,然后paginator将使用setMaxResults和setFirstResult扩展该查询,具体取决于请求中的参数

当您激发findBy或findByType时,结果是一个ArrayCollection,因此此时进行分页已经太晚了


也许我在这里遗漏了什么,你能再检查一下你提供的代码是否真的正常工作,但不是预期的吗?

我自己找到了解决方案。->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();

它工作正常唯一的问题是,正如我上面提到的,当我按下按钮移动到结果的第二页时,它禁用了过滤器。这是一件简单的事情,但我在文档中遗漏了它。感谢您指出我们需要使用GET。