PHP Symfony没有搜索结果

时间:2018-06-28 06:59:36

标签: php symfony search

我在搜索方面遇到问题,正在从事一个项目,以便用户可以根据邮政编码查找交货日期。荷兰和比利时都有邮政编码,荷兰邮政编码只是数字(例如1000),比利时的邮政编码前面是B(例如B1000)。当我搜索B1000时,它没有显示任何结果。希望你们能为我解决这两个问题。提前致谢。

控制器

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use AppBundle\Entity\Nlbelevering;

class PostcodezoekerController extends Controller
{

    /**
     * @Route ("/nlbe/levering", name="nlbelevering")
     */
    public function nlbeleveringHomepage(Request $request){
        $nlbelevering = $this->getDoctrine()->getRepository("AppBundle:Nlbelevering")->findAll();

        $search = $request->get('q');
        $em = $this->getDoctrine()->getManager();

        if ($search) {
            $nlbelevering = $em->createQuery('Select a FROM AppBundle:Nlbelevering a WHERE a.postcode LIKE :query')
                    ->setParameter('query', '%'.$search.'%');
        } else{
            $nlbelevering = $em->createQuery('Select a FROM AppBundle:Nlbelevering a WHERE a.postcode LIKE :query')
                    ->setParameter('query', '%'.$search.'%');
        }

        //Verwijzing naar formulier
        return $this->render('nlbe/levering/Nlbelevering.html.twig', [
            'nlbelevering' => $nlbelevering->getResult(),
            'q' => $search
        ]);
    }

}

?>

树枝

{% extends 'layout/default.html.twig' %}

{% block title %}NL/BE - Levering 30-33{% endblock %}

{% block content %}

    <style>
    .zoekformulier{

  padding: 20px;
  margin-top: 20px;
    }

    </style>


    <div class= "zoekformulier">
    <form>
        <input name="q" value="{{ q }}" placeholder="Zoeken" />
        <button type="submit">Zoeken</button>
    </form>
    </div>


        </div>
    </div>

    </div>
    </div>

    <div class="container-fluid">
        <div class="row">
            {% if q == 0 %}
               <div class="col-md-12" style="display:none"> 
            {% else %}
                <div class="col-md-12">
            {% endif %}
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th scope="col">Postcode</th>
                        <th scope="col">Week 30</th>
                        <th scope="col">Week 31</th>
                        <th scope="col">Week 32</th>
                        <th scope="col">Week 33</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for postcode in nlbelevering %}
                    <tr>
                        <th scope="row">{{ postcode.postcode }}</th>
                        <td>{{ postcode.week1 }}</td>
                        <td>{{ postcode.week2 }}</td>
                        <td>{{ postcode.week3 }}</td>
                        <td>{{ postcode.week4 }}</td>
                    </tr>
                    {% endfor %}
                    </tbody>
                </table>
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th scope="col">Postcode</th>
                        <th scope="col">Week 34</th>


        <th scope="col">Week 35</th>
                        <th scope="col">Week 36</th>
                        <th scope="col">Week 37</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for postcode in nlbelevering %}
                    <tr>
                        <th scope="row">{{ postcode.postcode }}</th>
                        <td>{{ postcode.week5 }}</td>
                        <td>{{ postcode.week6 }}</td>
                        <td>{{ postcode.week7 }}</td>
                        <td>{{ postcode.week8 }}</td>
                    </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
        <hr>
    </div>
{% endblock %}

搜索,没有严格匹配的内容

Search

搜索,没有结果

Search2

1 个答案:

答案 0 :(得分:1)

这是因为您的情况:比较一个字符串和一个0将在树枝中返回true,最好使用此代码:

R"((?:^|\\s)'|'(?!\\S))"