How to create sql OR WHERE condition in Zend Framework 2


As far as you probably know in ZF2 there is no “orWhere()” metod anymore.

In that case creating “OR WHERE” condition is a little bit tricky, but still doable without writing pure SQL code.
We will make use of so called Predicates


$select = new Select();
$select->from('tableName');

// This is where the magic happens
$select->where(array(
new Predicate\PredicateSet(
array(
new Predicate\Operator(‘first_field’, Predicate\Operator::OPERATOR_EQUAL_TO, $value1),
new Predicate\Operator(‘second_field’, Predicate\Operator::OPERATOR_EQUAL_TO, $value2),
),
Predicate\PredicateSet::COMBINED_BY_OR
)
));

That’s it! You can use couple of different types from Operator properties like “Operator::OPERATOR_GREATER_THAN” or “Operator::OPERATOR_LESS_THAN”.