doutan8506 2017-03-27 13:34
浏览 39

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

Whenever I check the "wetsoil" checkbox, my search results correctly filter through and display all the plant species that grow in wet soil, but whenever I don't check the checkbox, there are no search results. I want all the objects to appear if the checkbox is unchecked, not none of them. Not sure if my logic is incorrect, or maybe my syntax? In the database, "x" means yes.

My controller:

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,
        ));
    }

And my input on the HTML page:

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

Note: I chose not to use the standard Twig syntax ({{ form_widget(form.wetsoil) }}) due to other technical reasons.

Front end:

 <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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算
    • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
    • ¥20 有人知道这种图怎么画吗?
    • ¥15 pyqt6如何引用qrc文件加载里面的的资源
    • ¥15 安卓JNI项目使用lua上的问题
    • ¥20 RL+GNN解决人员排班问题时梯度消失
    • ¥60 要数控稳压电源测试数据
    • ¥15 能帮我写下这个编程吗
    • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路