Hancs Hancs - 3 months ago 7
SQL Question

Doctrine2 expressions eq special char for matching all

I am trying run filters with doctrine query builder, i am using expr eq, but if i don´t have filter value, i would like to use some special symbol for expr eq which returs all rows.

my code:

$q = $qb->select(array('p'))
->from(payment::class, 'p')
->innerJoin(customer::class, 'z', 'WITH', 'p.customer= z.id')
->where(
$qb->expr()->eq('z.id', '?2')
)
->setMaxResults($limit)
->setFirstResult($offset)
->orderBy('p.'.$sortField, $sortType)



there i want somting like(->setParameter(2, "*"))


->setParameter(2, $filtry['customer'])
->getQuery();

return $q->getResult();


Is it possible? I don´t want write different query builder for each filter.

Sorry for my English

Thanks

Answer

It's a query builder so you can add params conditionally, as example:

$qb->select(array('p'))
                ->from(payment::class, 'p')
                ->innerJoin(customer::class, 'z', 'WITH', 'p.customer= z.id')
                ->setMaxResults($limit)
                ->setFirstResult($offset)
                ->orderBy('p.'.$sortField, $sortType) 


if ($filtry['customer'])
{
$qb->andWhere(                            
              $qb->expr()->eq('z.id', $filtry['customer'])
                )
};

$q = $qb->getQuery()

Hope this help