Symfony KnpPaginator查询与表单字段中的自定义过滤器?

问题描述:

我正在创建一个修改KnpPaginatorBundle查询的表单,以显示过滤分页结果。Symfony KnpPaginator查询与表单字段中的自定义过滤器?

要做到这一点。当表单有效时,我使用必填字段构建查询字符串(连接)以进行过滤。我用我的自定义查询设置了$ 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

看一看日志文件,有什么确切的错误你得到了吗? – 2013-02-11 18:41:58

+0

当我使用** $ this->容器 - > ***方法并且收到500内部服务器错误。不记录任何新东西。使用dev或prod env。 :S当不试图使用参数的应用程序工作正常。但我无法得到我想要的行为。 – Jeflopo 2013-02-11 19:08:35

终于解决了它使用会话变量来存储当前的过滤器。会话应用程序范围广泛或无状态。我很满意这种方法: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 
     ) 
    ); 
} 
+0

请不要*逐字使用此代码。提供的代码不安全 - 具有sql注入漏洞。为了避免它,使用参数占位符(命名参数),而不是手动串联值与DQL。另外教条查询生成器有更好的API来构建查询。 另外我看到没有理由在会话中保留生成的DQL – jacekll 2017-10-25 18:07:00