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?
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.'%')
;
try this:
return $this->createQueryBuilder("a")
->andWhere('a.id like :id' OR 'a.client like :client')
->setParameters([
'id' => '%' . $filter . '%',
'client' => '%' . $filter . '%'
]),
->getQuery();
You should use orWhere
instead of andWhere
public function findFilter($filter)
{
return $this->createQueryBuilder("a")
->where('a.id like :filter')
->orWhere('a.client like :filter')
->setParameter('filter', '%' . $filter . '%')
->getQuery()
;
}
Moreover you might have missed a getResult
after getQuery
(but don't know if it is a typo or something else)