Fabian
Fabian

Reputation: 183

How to combine where statements in Zend Framework 2

That may be a silly question, but I just can't find any answer to it.

I have a simple query like 'SELECT * FROM table WHERE (x=a AND y=b) OR z=c' and I just can't find out how to implement that in ZF2.

I found a lot of information on Predicates and Where objects, but I can't find out how to combine that information into a query consisting of AND and OR.

I'd really appreciate it if someone can point me to the right direction.

Upvotes: 1

Views: 947

Answers (1)

Andrew
Andrew

Reputation: 12809

If you want to add them using AND, you can simply pass them into the select object using an array, for example:

$select->where(array(
    'type'         => 'test,
    'some_field'   => '1'
));

If you want more complex Where queries, then you can use a Where object to build up a query:

$where = new \Zend\Db\Sql\Where();
$where->addPredicate(
        new \Zend\Db\Sql\Predicate\Like('some_field', '%'.$value.'%')
)
->OR->->equalTo('my_field', 'bob')
->AND->equalTo('my_field', 'hello');

$select->where($where);

Upvotes: 3

Related Questions