12

have such zend query:

$select = $this->_table
               ->select()
               ->where('title LIKE  ?', '%'.$searchWord.'%')
               ->where('description LIKE  ?', '%'.$searchWord.'%')
               ->where('verified=1 AND activated=1');

In other words it looks like:

SELECT `some_table`.* FROM `some_table` WHERE (title LIKE '%nice house%') AND (description LIKE '%nice house%') AND (verified=1 AND activated=1)

If I have couple AND sentences, zend connect it through AND operator. How can I connect it with OR operator ? Cause I need:

...(title LIKE '%nice house%') OR (description LIKE '%nice house%')...

Your help would be appreciated.

Charles
  • 50,943
  • 13
  • 104
  • 142
Bounce
  • 2,066
  • 6
  • 34
  • 65
  • I think just sql.. operator precedence will be taken into account, coz these queries will be converted to plain sql. I am just suggesting as I too have this doubt and came here – Rohith Raveendran Jul 02 '13 at 09:46

3 Answers3

26
$this->_table->select()->orWhere($condition);

To build more complexe queries (i.g. sub-conditions) you might have to use $this->table->getAdapter()->quoteInto() and write your SELECT manually.

Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
5

The others answers here do not work (anymore?), the current solution is to call nest() and unnest() in the where clause:

$select->where
    ->nest()
        ->equalTo('column1', 1)
        ->or
        ->equalTo('column2', 2)
    ->unnest()
    ->and
    ->equalTo('column3', 3);
Andreas
  • 2,821
  • 25
  • 30
1

I have implemented your zend query

$select = $this->_table
               ->select()
               ->where('title LIKE  ?', '%'.$searchWord.'%')
               ->ORwhere('description LIKE  ?', '%'.$searchWord.'%')
               ->where('verified=1 AND activated=1');
SagarPPanchal
  • 9,839
  • 6
  • 34
  • 62