簡體   English   中英

Symfony2中的搜索功能不起作用

[英]Search Function in Symfony2 is not working

我正在使用symfony框架進行項目。 我只想做一個簡單的搜索功能,它可以搜索用戶輸入內容中包含的所有實體。 當我嘗試運行我的代碼時,它無法正常工作,即使它們是與之相關的文件,它也會繼續重定向到未找到的結果。

這是我的控制器:

public function searchAction(Request $request){
        $request = $this->getRequest();
        $data = $request->request->get('search');


        $em = $this->getDoctrine()->getManager();
        $query = $em->createQuery(
                'SELECT a,b,c
                FROM SampleBundle:transactionDetail a
                JOIN a.transaction b
                JOIN b.documentRelated c
                WHERE a.pNumber LIKE :data
                AND b.senderId LIKE :data
                AND b.receiverId LIKE :data
                AND b.transactDate LIKE :data
                AND a.amountPaid LIKE :data')
                ->setParameter('data',$data);  

        $res = $query->getResult();
        if($res == null){
                return $this->render('SampleBundle:Sample:noresult.html.twig');
        }
        return $this->render('SampleBundle:Sample:search.html.twig', array('res' => $res));
    }

和我的search.html.twig

{% extends '::layout.html.twig' %}
{% block pageTitle %} Related Files Found{% endblock %}
{% block body %}

div class="table-responsive margins" >
  <table  class="table table-condensed table-striped table-bordered table-hover no-margin">
      <thead>
        <tr style="height: 40px; ">
        <th>Transaction Date</th>       
        <th>Sender ID</th> 
        <th>Receiver ID</th>
        <th>P Number</th> 
        <th>Amount Paid</th> 

      </tr>
    </thead>
    <tbody>
    <tr>    
        {% for res in res %}         
          {% for other in res.transaction %}
         <td >{{res.transaction.transactDate|date('YMd')}}&nbsp;&nbsp;{{res.ediTransaction.transactionDate|date('H:i')}}</td>
          <td>{{res.transaction.senderId}}</td>
          <td >{{res.transaction.receiverId}}</td>

          {% if other.pNumber != null %}
            <td style="word-break: break-all">{{other.pNumber}}</td> 
          {% else %}
            <td>N/A</td>
          {% endif %}
          {% if other.amountPaid != null %}
             <td style="word-break: break-all">{{other.amountPaid}}</td>    
          {% else %}
            <td>N/A</td>
          {% endif %}

      </tr>
          {% endfor %}
      {% endfor %}
      </tbody>
    </table>
  </div>
{% endblock %}
{% block javascripts %}
  {% javascripts
    'bundles/sampledoc/js/jQuery.js'
    'bundles/sampledoc/js/bootstrap.js'
     %}
    <script src="{{ asset_url }}"></script>

  {% endjavascripts %}
{% endblock %}

我的搜索表格:

<form action="{{path('sample_search')}}" method="POST">
  <div class="form-group input-group">
    <input type="text" name="search" class="form-control" placeholder="Search" style="width: 200px; float: right;" >
      <span class="input-group-btn"><button class="btn btn-default" type="submit" style="margin-right: 20px;"><i class="fa fa-search"></i></button></span>
  </div>
</form>

有人可以幫我嗎? 提前致謝。

數據不能等於您的所有條件。 如果您AND查詢數據中的所有內容都必須匹配所有字段,那么您很可能不會返回任何結果。

嘗試類似:

WHERE a.pNumber LIKE :data
OR b.senderId LIKE :data
OR b.receiverId LIKE :data
OR b.transactDate LIKE :data
OR a.amountPaid LIKE :data')
->setParameter('data', "%$data%"); 

並查看生成的SQL

$query->getSQL()

這樣,您實際上可以直接在數據庫上測試查詢。

假設$ data是一個包含所有優點的關聯數組,我認為您不能按照自己的方式將其交給setParameter()。 您需要針對每個“ LIKE”子句更具體地對其進行細分。

完全未經測試,但我懷疑您正在尋找類似的東西:

`
WHERE a.pNumber LIKE :pNumber
AND b.senderId LIKE :senderId
AND b.receiverId LIKE :recieveId
AND b.transactDate LIKE :transactDate
AND a.amountPaid LIKE :amountPaid')
->setParameters(array(
    'pNumber' => $data['pNumber'],
    'receiverId'   => $data['receiverId'],
    'transactDate' => $data['transactDate'],
    'amountPaid'   => $data['amountPaid'],
));
`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM