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>