我正在创建这样的表单搜索:
当我运行搜索时,它会向我显示包含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();
}
}
谢谢大家,
答案 0 :(得分: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
);
不确定它是否有效,但请尝试一下。