Symfony 2 - Null复选框不返回搜索结果

时间:2017-03-27 13:34:06

标签: php mysql database symfony doctrine

每当我检查" wetsoil"复选框,我的搜索结果正确地过滤并显示在潮湿土壤中生长的所有植物物种,但每当我不检查复选框时,都没有搜索结果。如果取消选中该复选框,我希望所有对象都出现,而不是没有。不确定我的逻辑是不正确的,还是我的语法?在数据库中," x"是的。

我的控制器:

public function SearchResultsAction(Request $request)
    {
        $botanicalname = $request->query->get('botanicalname');
        $commonname = $request->query->get('commonname');
        $wetsoil = $request->query->get('wetsoil');

        $repository = $this->getDoctrine()->getRepository('AppBundle:Shrubs');

        $query = $repository->createQueryBuilder('p');

        $shrubs = $query
            ->where($query->expr()->like('p.botanicalname',':botanicalname'))
            ->setParameter('botanicalname', '%'.$botanicalname.'%')
            ->andwhere($query->expr()->like('p.commonname',':commonname'))
            ->setParameter('commonname', '%'.$commonname.'%')
            ->andWhere($query->expr()->like('p.wetsoil',':wetsoil'))
            ->setParameter('wetsoil', $wetsoil)
            ->orderBy('p.commonname', 'ASC')
            ->getQuery()
            ->getResult();

        return $this->render('shrubs/searchresults.html.twig', array(
            'shrubs' => $shrubs,
        ));
    }

我在HTML页面上的输入:

          <label for="wetsoil">Tolerates Wet Soil: </label>
          <input type="checkbox" name="wetsoil" value="x">

注意:由于其他技术原因,我选择不使用标准的Twig语法({{form_widget(form.wetsoil)}})。

前端:

 <table class="table table-striped">
                <thead>
                {% if shrubs | length == 0 %}
                <h3>No species match your search criteria. <a href="{{ path('shrubs_search')}}">Try again.</a></h3>
                {% else %}
                <tr>
                    <th>Common Name</th>
                    <th>Botanical Name</th>
                </tr>
                </thead>
                <tbody>
                <tr>

                    {% endif %}
                    {% for shrub in shrubs %}
                    <td><a href="{{ path('shrubs_show', { 'id': shrub.number }) }}">{{ shrub.commonname }}</a></td>
                    <td><a href="{{ path('shrubs_show', { 'id': shrub.number }) }}">{{ shrub.botanicalname }}</a></td>
                    {#<td>#}
                        {#<ul>#}
                            {#<li>#}
                                {#<a href="{{ path('shrubs_show', { 'id': shrub.number }) }}">show</a>#}
                            {#</li>#}
                            {#<li>#}
                                {#<a href="{{ path('shrubs_edit', { 'id': shrub.number }) }}">edit</a>#}
                            {#</li>#}
                        {#</ul>#}
                    {#</td>#}
                </tr>
                {% endfor %}
                </tbody>
            </table>

0 个答案:

没有答案