我正在创建一个修改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
)
);
}
答案 0 :(得分:0)
最后使用会话变量解决它以存储当前过滤器。会话是应用程序范围或无状态。我很满意这种方法: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
)
);
}