Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OR clause in Zend DB update?

I'd like to do a Zend db update with an OR clause. What would be the equivalent statement to:

UPDATE mail
SET message_read = 1
WHERE id = 5
OR id = 10
like image 647
joeschmidt45 Avatar asked Jun 27 '12 22:06

joeschmidt45


2 Answers

When calling Zend_Db_Adapter::update(), multiple WHERE conditions will automatically be combined using AND (line 698 of Zend/Db/Adapter/Abstract.php in function _whereExpr).

You can get around this by creating your own Zend_Db_Expr which you will use as the WHERE condition and it will be left untouched.

For example:

$where[] = new Zend_Db_Expr(
        $table->getAdapter()->quoteInto('id = ?', 5) . ' OR ' .
        $table->getAdapter()->quoteInto('id = ?', 10)
);

// resulting expression:
//   WHERE (id = 5 OR id = 10)

$table->update($data, $where);

If you had additional WHERE conditions, they would be combined with the OR condition by an AND.

Example:

$where[] = new Zend_Db_Expr(
        $table->getAdapter()->quoteInto('id = ?', 5) . ' OR ' .
        $table->getAdapter()->quoteInto('id = ?', 10)
);
$where[] = $table->getAdapter()->quoteInto('type = ?', 'sometype');

// resulting expression:
//   WHERE (id = 5 OR id = 10) AND (type = 'sometype')
like image 62
drew010 Avatar answered Nov 12 '22 07:11

drew010


->where() will add a where clause to the query and will put an 'AND'. There is an orWhere method that exists to do that.

$select = $this->select();
$select->where('id = 5');
$select->orWhere('id = 10');

$this->fetchAll($select);
like image 32
Joel Lord Avatar answered Nov 12 '22 09:11

Joel Lord