Search code examples
zend-frameworkzend-dbzend-db-table

How do I add complex where clause to Zend Table Select?


I searched the Web and could not find anything that would show me a good solid example. My question is basically this:

How do I convert this:

SELECT * FROM table WHERE ((a = 1 AND b = 2) OR (c = 3 OR c = 4)) AND d = 5;

To Zend syntax similar to this:

$this ->select() ->from($this->_schema.'.'.$this->_name) ->where('a = ?', '1');

So how can it be done?

Thank a lot in advance.


Solution

  • I had a similar problem. See the code example in the answer here: Grouping WHERE clauses with Zend_Db_Table_Abstract

    So you would end up with something like:

    $db = $this->getAdapter();
    $this->select()
         ->where('(' . $db->quoteInto('a = ?', 1) . ' AND ' . $db->quoteInto('b = ?', 2) . ') OR (' . $db->quoteInto('c = ?', 3) . ' OR ' . $db->quoteInto('c = ?', 4) . ')')
         ->where('d = ?', 5);
    

    Which would give you:

    SELECT `table_name`.* FROM `table_name` WHERE ((a = 1 AND b = 2) OR (c = 3 OR c = 4)) AND (d = 5)