我正在创建一个表单,该表单修改KnpPaginatorBundle的查询,以便显示筛选后的分页结果。
这样做。当表单有效时,我使用要筛选的必需字段构建查询字符串(串联)。我用自定义查询设置了$filteredDql变量。问题是它的值只保留在第一页。当我更改页面时,它将变为NULL。分页重置。。。
我认为,问题可能是我在块上下文中设置了$filteredDql变量(当表单仅有效时)。
如何为整个操作或应用程序范围设置$filteredDql变量?也许使用参数?我尝试从控制器使用容器,但没有成功,使用:
$this->container->setParameter('key', value);
$this->container->getParameter('key');
$this->container->HasParameter('key');
但通过这种方式,我得到500内部服务器错误
这是代码:
public function indexAction(Request $request, $page)
{
$filters = new Filters();
$form = $this->createForm(new FiltersType(), $filters);
$dql = "SELECT a FROM ViciousAmateurBundle:Post a WHERE a.is_active = true";
if ($request->isMethod('POST')) {
$form->bind($request);
if ($form->isValid()) {
$country = $filters->getCountry();
$city = $filters->getCity();
$gender = $filters->getGender();
$sexualOrientation = $filters->getSexualOrientation();
if (isset($country)) {
$dql .= " AND a.country = '" . $filters->getCountry() . "'";
}
if (isset($city)) {
$dql .= " AND a.city = '" . $filters->getCity() . "'";
}
if (isset($gender)) {
$dql .= " AND a.gender = '" . $filters->getGender() . "'";
}
if (isset($sexualOrientation)) {
$dql .= " AND a.sexual_orientation = '" . $filters->getSexualOrientation() . "'";
}
$filteredDql = $dql;
}
}
$em = $this->get('doctrine.orm.entity_manager');
if (isset($filteredDql)) {
$query = $em->createQuery($filteredDql);
} else {
$query = $em->createQuery($dql);
}
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$query,
$this->get('request')->query->get('page', $page),
5
);
return $this->render('ViciousAmateurBundle:Default:index.html.twig', array(
'form' => $form->createView(),
'pagination' => $pagination
)
);
}
最后使用会话变量来存储当前过滤器来解决问题。会话是应用程序范围的或无状态的。我对这种方法很满意:D
尽管如此,令人遗憾的是,当数据来自表单验证时,KnpPaginatorBundle无法记住默认情况下通过分页的自定义查询"过滤器"(WHERE,GROUP BY,…):s并且只通过使用可排序函数使链接进行asc或desc排序来支持它。。。
这是代码:
/**
* @Route("/{page}", defaults={"page" = 1}, name="homepage")
* @Route("/")
* @Template()
*/
public function indexAction(Request $request, $page)
{
$filters = new Filters();
$form = $this->createForm(new FiltersType(), $filters);
$session = $this->getRequest()->getSession();
if ($session->get('dql') == null) {
$session->set('dql', "SELECT a FROM ViciousAmateurBundle:Post a WHERE a.is_active = true");
}
if ($request->isMethod('POST')) {
$form->bind($request);
if ($form->isValid()) {
$dql = "SELECT a FROM ViciousAmateurBundle:Post a WHERE a.is_active = true";
$country = $filters->getCountry();
$city = $filters->getCity();
$gender = $filters->getGender();
$sexualOrientation = $filters->getSexualOrientation();
if (isset($country)) {
$dql .= " AND a.country = '" . $filters->getCountry() . "'";
}
if (isset($city)) {
$dql .= " AND a.city = '" . $filters->getCity() . "'";
}
if (isset($gender)) {
$dql .= " AND a.gender = '" . $filters->getGender() . "'";
}
if (isset($sexualOrientation)) {
$dql .= " AND a.sexual_orientation = '" . $filters->getSexualOrientation() . "'";
}
$session->set('dql', $dql);
}
}
$em = $this->get('doctrine.orm.entity_manager');
$query = $em->createQuery($session->get('dql'));
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$query,
$this->get('request')->query->get('page', $page),
5
);
return $this->render('ViciousAmateurBundle:Default:index.html.twig', array(
'form' => $form->createView(),
'pagination' => $pagination
)
);
}