Reputation: 2557
I want to add orWhere conditions to query builder. params will come from array. but I dont know how I can add count of orWhere statements equal to count array elements. Here is some code:
public function selectRelatedTrips($assoc)
{
$params = array('k' => 'Kiev');
$query = $this
->getEntityManager()
->createQueryBuilder()
->select('t')
->from('VputiTripBundle:Trip', 't')
->where('t.startCity = :k');
foreach ($assoc as $k => $v) {
// $query->orWhere('t.startCity = ' . $v);
$query->where('t.startCity = :param' . $k);
$params['param' . $k] = $v;
}
return $query->setParameters($params)
->setMaxResults(20)
->orderBy('t.id', 'desc')
->getQuery()
->getResult();
}
Can somebody help me please?
Upvotes: 1
Views: 268
Reputation: 10483
You can use the in()
function:
class AcmeRepository extends EntityRepository
{
public function selectRelatedTrips($assoc)
{
$qb = $this->createQueryBuilder('e');
$qb = $qb
->where(
$qb->expr()->in('e.field2', array_keys($assoc))
);
return($qb->getQuery()->getResult());
}
}
See How to use WHERE IN with Doctrine 2 and Doctrine2 Query Builder for references.
Upvotes: 2
Reputation: 1167
Try to build the query dinamically, based by your array:
$params = array('param1' => 123);
$query = $this
->createQueryBuilder('e')
->select('e')
->where('e.field = :param1')
foreach ($assoc as $k => $v) {
$query->orWhere('e.field2 = :param'.$k);
$params['param'.$k] = $v;
}
$query->setParameters($params);
Upvotes: 1