Possible Duplicate:
Zend Framework Complex Where Statement
How do I add something like this to a Zend_Db_Select while still using quoting?
SELECT x FROM y WHERE (a=1 OR b=2) AND (c=3 OR d=4)
I have seen where() and orWhere(), but I see no way of grouping them with brackets. I realize that I could just write this:
->where('(a=1 OR b=2) AND (c=3 OR d=4)')
But ideally I would like to have 4 separate calls to where() to be able to use the second parameter for quoting (assuming the values 1, 2, 3 etc. are user generated input).
Is there any way to do this in the pure fluent style?
Is there any way to do this in the pure fluent style?
Short answer: no.
As you mention, you can use orWhere
to write conditions like (a = ?) OR (b = ?)
.
Zend_Db_Select automatically puts parentheses around each expression you specify using the where() or orWhere() methods. This helps to ensure that Boolean operator precedence does not cause unexpected results.
However it is not possible to nest AND
and OR
conditions in the way you wish.
If you wrote this:
->where('a=?', $a)
->orWhere('b=?', $b)
->where('c=?', $c)
->orWhere('d=?', $d)
It would result in SQL similar to this:
(a=1) OR (b=2) AND (c=3) OR (d=4)
Which would be interpreted as follows (not what you want):
(a=1) OR ((b=2) AND (c=3)) OR (d=4)
Alternatives:
quoteInto
as demonstrated here.