【问题标题】:pagination with filter knp paginator使用过滤器 knp 分页器进行分页
【发布时间】:2019-07-22 12:41:13
【问题描述】:

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

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

【问题讨论】:

    标签: symfony pagination knppaginator


    【解决方案1】:

    问题在于您执行了查询(分页器没有抱怨这一点实际上很奇怪......应该有一个错误,如“期望类型查询,得到 ArrayCollection”或类似的东西。

    分页器希望您传递一个Query 对象,然后分页器将使用setMaxResultssetFirstResult“扩展”该查询,具体取决于请求中的参数。

    当您触发findByfindByType 时,结果是ArrayCollection,因此此时分页已经“为时已晚”。

    也许我在这里遗漏了一些东西,您能否再次检查您提供的代码是否真的“有效,但未达到预期”?

    【讨论】:

    • 它工作正常,唯一的问题是正如我上面提到的,当我按下按钮移动到结果的第二页时它会禁用过滤器
    【解决方案2】:

    我自己找到了解决方案。 ->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。
    猜你喜欢
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    • 2021-10-19
    • 1970-01-01
    • 2016-07-01
    • 2018-12-25
    • 1970-01-01
    相关资源
    最近更新 更多