Zend_Db_Select does not provide an interface to all
possible boolean constructions. For example, it does not support NOT
predicates or nested boolean expressions.
Currently the solution to do what you describe is the
$select = $db0->select()
->where('a = ?', 'x')
->where('b = ?', 'y')
->where("c = 'a' OR d =
In other words, any expressions deeper than what is
supported by the current where() and orWhere() methods is up to you to construct
as a string.
Note that Zend_Db_Select does put parentheses around AND
terms, so if the expression within your term contains an OR, you don't get
surprised by operator precedence issues.
IMO, a programmatic interface to constructing deeper
boolean expressions does not have enough value to justify the complexity of
creating it. It makes it harder to write code and harder to read
code. Complex boolean expressions must be written as
Hypothetically, if I were to design a solution to do this
programmatically, it might have the usage below:
The where() optionally would accept an array argument. The
array would be processed as terms that should be joined with the AND
// produces (a = 'x') AND (b =
->where(array("a = 'x'", "b = 'y'"))
If an array element were itself an array, its elements
would be joined with an OR operation and then the result joined to the top array
// produces (a = 'x') AND (b = 'y') AND ((c = 'a')
OR (d = 'a'))
If an element of the inner array were an array, its
elements would be joined with AND and then the result processed as an OR
term. Each level of nested array would alternate to the complementary
The usage of orWhere() would be similar. Each level
of nested array would alternate just as in the where() method, but the top-level
would start with the OR operator.
However, this solution would not support the quoted
parameter processing currently supported by where() and orWhere(). You'd
have to use quoteInto(). Also, it would have no programmatic interface
for the boolean NOT operation.
Again, for readers who skim these messages instead of
reading them carefully: THIS IS AN HYPOTHETICAL DISCUSSION.
ZEND_DB_SELECT DOESN'T SUPPORT THE USAGE DESCRIBED ABOVE.
Try this to eliminate the ugly "( 1=1 )" hack.
Also you should compare the keyword using "false !==" in case the keyword is a
value that evaluates to false, like empty string or 0. And I would quote
the identifiers for the search fields in case they are SQL