Zend-framework DB: OR instead of AND operator
Asked Answered
M

3

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.

Matri answered 21/8, 2010 at 7:40 Comment(1)
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 hereGlosseme
N
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.

Nitre answered 21/8, 2010 at 7:45 Comment(0)
Z
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);
Zucker answered 28/5, 2016 at 7:10 Comment(1)
That syntax looks to be for Zend FW 2.2. framework.zend.com/apidoc/2.2/classes/…Apocalyptic
W
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');
Wane answered 31/1, 2014 at 6:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.