I added a filter to my project that works with symfony.
I have the ID (numbers to be searched) and the client name. When I constructed my query with one parameter, it works, just like that
public function findFilter($filter)
{
return $this->createQueryBuilder("a")
->andWhere('a.id like :id')
->setParameter('id', '%' . $filter . '%')
->getQuery()
;
}
and when I add one more parameter, the search doesn't happen.
public function findFilter($filter)
{
return $this->createQueryBuilder("a")
->andWhere('a.id like :id')
->setParameter('id', '%' . $filter . '%')
->andWhere('a.client like :client')
->setParameter('client', '%' . $filter . '%')
->getQuery()
;
}
and here is my view where the filter can be entered
<form action="" method="get">
<input name="filter" type="text">
<button type="submit" class="btn btn-default">Filtrer</button>
</form>
So maybe I'm not sticking them right? Anyone got an idea on how to add more parameters for the filter bar?
try this:
return $this->createQueryBuilder("a")
->andWhere('a.id like :id' OR 'a.client like :client')
->setParameters([
'id' => '%' . $filter . '%',
'client' => '%' . $filter . '%'
]),
->getQuery();
If you want to filter on multiple columns with an unique value, you've to use an array of OR filters.
use Doctrine\ORM\Query\Expr;
[...]
$orX = new Expr\Orx();
$orX->add($qb->expr()->orx($qb->expr()->like('a.id', ':filter'));
$orX->add($qb->expr()->orx($qb->expr()->like('a.client', ':filter'));
$qb
->andWhere($orx)
->setParameter('filter', '%'.$filter.'%')
;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With