【发布时间】: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