2017-06-14 28 views
0

我是Symfony的新手,我試圖用搜索字段過濾我的表格。我使用KnpPaginatorBundle對錶格進行分頁和排序,並且我創建了一個表單來過濾我的請求(方法GET)Symfony使用空格和下劃線獲取url參數

它通常工作,但是當我在搜索輸入中使用空格或下劃線時,它不會沒有工作,我認爲有關GET方法和編碼文本的方法有些事情要做,但我不知道如何。

這裏是我的代碼:

查看:

<div class="row"> 
     <div class="well row"> 
      <form action="" method="get"> 

       <div class="col-md-2"> 
        <label for="famille">Famille d'articles</label> 
        <select name="famille"> 
         <option value="0">Toutes</option> 
         {% for famille in listFamilles %} 
          <option value="{{ famille.id }}" {% if data.famille is defined %} {% if famille.id == data.famille %} selected {% endif %} {% endif %}>{{ famille.nom }}</option> 
         {% endfor %} 
        </select> 
       </div> 

       <div class="col-md-4"> 
        <input type="checkbox" name="rds" {% if data.rds == 1 %} checked {% endif %}> 
        <label for="rds" style="margin-left:5px">Montrer les articles en rupture de stock</label> 
       </div> 

       <div class="col-md-4"> 
        <label for="recherche">Recherche</label> 
        <input name="recherche" style="width:100%" type="text" placeholder="Recherche" {% if data.recherche is defined %} value="{{ data.recherche }}" {% endif %}> 
       </div> 

       <div class="col-md-2" style="text-align:center"> 
        <button type="submit" class="btn btn-primary">Rechercher</button> 
       </div> 

      </form> 
     </div> 
     <div class="well row"> 
      <table class="table table-bordered table-striped" style="width: 100%" cellspacing="0"> 
       <thead> 
        <tr> 
         <th>{{ knp_pagination_sortable(listArticles, 'Référence client', 'a.ref_article') }}</th> 
         <th>{{ knp_pagination_sortable(listArticles, 'Référence interne', 'a.ref_logistique') }}</th> 
         <th>{{ knp_pagination_sortable(listArticles, 'Famille', 'f.nom') }}</th> 
         <th>{{ knp_pagination_sortable(listArticles, 'Libellé', 'a.libelle') }}</th> 
         <th>{{ knp_pagination_sortable(listArticles, 'Alerte', 'a.stock_alerte') }}</th> 
         <th>{{ knp_pagination_sortable(listArticles, 'Stock', 'a.stock_actuel') }}</th> 
        </tr> 
       </thead> 
       <tbody id="bodyListeArticles"> 
        {% for article in listArticles %} 
         <tr> 
          <td><a href="{{ path('gr_bo_modif_article', {'article_id': article.id}) }}">{{ article.refArticle }}</a></td> 
          <td>{{ article.refLogistique }}</td> 
          <td>{{ article.famille.nom }}</td> 
          <td>{{ article.libelle }}</td> 
          <td>{{ article.StockAlerte }}</td> 
          <td>{{ article.StockActuel }}</td> 
         </tr> 
        {% endfor %} 
       </tbody> 
      </table> 

      <div class="navigation text-center"> 
       {{ knp_pagination_render(listArticles) }} 
      </div> 

     </div> 

    </div> 

控制器:

public function listeAction(Request $request) { 
     if ($this->get('security.authorization_checker')->isGranted('ROLE_OPERATEUR')) { 
      $session = $request->getSession(); 
      if ($session->get('client_id')) { 
       $clientId = $session->get('client_id'); 
      } else { 
       $request->getSession()->getFlashBag()->add('info', 'Vous devez sélectionner un client pour accéder à la liste de ses articles.'); 
       return $this->redirectToRoute('gr_bo_liste_clients'); 
      } 
     } elseif ($this->get('security.authorization_checker')->isGranted('ROLE_SUPERCOLLABORATEUR') || ($this->get('security.authorization_checker')->isGranted('ROLE_COLLABORATEUR') && $this->getUser()->getListeArticles())) { 
      $clientId = $this->getUser()->getClient()->getId(); 
     } else { 
      $request->getSession()->getFlashBag()->add('info', 'Vous n\'avez pas les permissions requises pour accéder à cette page.'); 
      return $this->redirectToRoute('gr_bo_liste_commandes'); 
     } 
     $em = $this->getDoctrine()->getManager(); 
     $data = []; 
     $data['clientId'] = $clientId; 

     if ($request->query->getAlnum('recherche')) { 
      $data['recherche'] = $request->query->getAlnum('recherche'); 
     } 

     if ($request->query->getAlnum('famille') && $request->query->getAlnum('famille') != "0") { 
      $data['famille'] = $request->query->getAlnum('famille'); 
     } 


     if ($request->query->getAlNum('rds') == "on" || ($request->query->getAlnum('rds') == "" && $request->query->getAlnum('famille') == "" && $request->query->getAlnum('recherche') == "")) { 
      $data['rds'] = 1; 
     } else { 
      $data['rds'] = 0; 
     } 

     $listArticles = $em->getRepository('GRBackOfficeBundle:Article')->getQueryArticles($data); 

     /** 
     * @var $paginator \Knp\Component\Pager\Paginator 
     */ 
     $paginator = $this->get('knp_paginator'); 
     $result = $paginator->paginate(
       $listArticles, $request->query->getInt('page', 1), $request->query->getInt('limit', 5) 
     ); 

     $listFamilles = $em->getRepository('GRBackOfficeBundle:Famille')->findAll(); 

     return $this->render('GRBackOfficeBundle:Article:liste_articles.html.twig', array(
        'listArticles' => $result, 
        'listFamilles' => $listFamilles, 
        'data' => $data 
     )); 
    } 

庫:

public function getQueryArticles($data) { 
     $query = $this->createQueryBuilder('a') 
       ->leftJoin('a.images', 'i') 
       ->addSelect('i') 
       ->leftJoin('a.type_stockage', 't') 
       ->addSelect('t') 
       ->leftJoin('a.famille', 'f') 
       ->addSelect('f'); 
     if (array_key_exists('famille', $data)) { 
      $query->andWhere('f.id = :famille') 
        ->setParameter('famille', $data['famille']); 
     } 
     if (array_key_exists('rds', $data)) { 
      if ($data['rds'] == 0) { 
       $query->andWhere('a.stock_actuel > 0'); 
      } 
     } 
     if (array_key_exists('recherche', $data)) { 
      $query->andWhere('a.ref_article LIKE :recherche OR a.ref_logistique LIKE :recherche OR a.libelle LIKE :recherche') 
        ->setParameter('recherche', '%' . $data['recherche'] . '%'); 
     } 
     $query->leftJoin('a.sousfamille', 's') 
       ->addSelect('s') 
       ->leftJoin('a.client', 'c') 
       ->addSelect('c') 
       ->andWhere('c.id = :client') 
       ->setParameter('client', $data['clientId']) 
       ->orderBy('a.ref_article', 'ASC') 
       ->getQuery(); 


     return $query; 
    } 

當我使用空格或下劃線我「搜索過濾器「 r,我的表格顯示爲空,並且它似乎刪除空格或下劃線,所以如果嘗試使用「John Doe」或「John_Doe」,它將返回「JohnDoe」的結果,該結果爲空。

如果有人知道我如何繼續下去,將不勝感激!

回答

1

您可以在您的data.recherche上使用urlencode。但也有更自然的方式來做到這一點twig

+0

我試圖改變我的輸入是這樣的:''但它並沒有改變 – AKM

+1

當你發送表單時,URL中的參數是否被轉義?問題也可能與[getAlnum](http://api.symfony.com/2.8/Symfony/Component/HttpFoundation/ParameterBag.html#method_getAlnum)方法有關,因爲根據doc它不返回空格和下劃線(只有數字和字符)。您可以嘗試使用[獲取](http://api.symfony.com/2.8/Symfony/Component/HttpFoundation/ParameterBag.html#method_get)。 –

+0

謝謝!隨着一個簡單的得到,它工作正常:D – AKM