Multiple/nested "select where" with Zend_Db_Select

Posted by DJRayon on Stack Overflow See other posts from Stack Overflow or by DJRayon
Published on 2010-05-08T19:32:34Z Indexed on 2010/05/08 19:38 UTC
Read the original article Hit count: 203

Hi there

I need to create something like this:

select name from table where active = 1 AND (name LIKE 'bla' OR description LIKE 'bla')

The first part is easy:

$sqlcmd = $db->select()
->from("table", "name")
->where("active = ?", 1)

Now comes the tricky part. How can I nest? I know that I can just write

->orWhere("name LIKE ? OR description LIKE ?", "bla")

But thats wron, because I need to dynamically change all the parts. The query will be built all the time the script runs. Some parts get deleted, some altered. In this example I need to add those OR-s because sometimes I need to search wider. "My Zend Logic" tells me that the correct way is like this:

$sqlcmd = $db->select()
->from("table", "name")
->where("active = ?", 1)
->where(array(
    $db->select->where("name LIKE ?", "bla"),
    $db->select->orWhere("description LIKE ?", "bla")
))

But that doesn't work (atleast I dont remember it working).

Please. Can someone help me to find a object oriented way for nesting "where"-s

© Stack Overflow or respective owner

Related posts about zend-framework

Related posts about zend-db