1

我正在创建这样的表单搜索:

当我运行搜索时,它向我显示了包含 10 个元素的第一页,但是当我单击第 2 页时,它显示了我数据库的所有元素,而不考虑我的条件搜索。我不明白

class FrontController extends Controller
{
       /**
        * @Route("/", name="front_homepage")
        */
        public function indexAction(){

             $form = $this->createForm(SearchType::class);

             return $this->render('FrontBundle::layout.html.twig', array(
                 'form' => $form->createView(),
             ));
        }

}


class SearchController extends Controller
{
    /**
     * @param Request $request
     * @Route("/search/form", name="front_search_form")
     */
    public function searchOffreAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();

        $form = $this->createForm(SearchType::class);

        $form->handleRequest($request);

        if($form->isSubmitted()) {

           $entity = $em->getRepository('CoreBundle:Foo')->findByCritere($form->getData());

            /** @var Paginator $paginator */
            $paginator  = $this->get('knp_paginator');
            $result = $paginator->paginate(
                $entity,
                $request->query->getInt('page', 1),
                10
            );

            return $this->render('FrontBundle:Search:offre.html.twig', ['offres' => $result]);

        }

       return $this->createNotFoundException();
    }

}

谢谢你们,

4

1 回答 1

1

我认为问题是您$entity需要成为查询而不是结果。

试试这个:

$em = $this->getDoctrine()->getManager();
$dql   = "SELECT f FROM CoreBundle:Foo f";
$query = $em->createQuery($dql);

/** @var Paginator $paginator */
$paginator  = $this->get('knp_paginator');
$result = $paginator->paginate(
     $query,
     $request->query->getInt('page', 1),
     10
);

不确定它是否会起作用,但请尝试一下。

于 2016-06-17T15:18:38.170 回答