5

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
joeschmidt45
  • 1,932
  • 2
  • 17
  • 20
  • possible duplicate of [How to Use Multiple Conditions In An Update Statement With Zend_Db And QuoteInto](http://stackoverflow.com/questions/6321504/how-to-use-multiple-conditions-in-an-update-statement-with-zend-db-and-quoteinto) – cmbuckley Jun 27 '12 at 22:57
  • 1
    @cbuckley This would combine the WHERE using `AND` which is not what the OP wants. – drew010 Jun 27 '12 at 23:16

2 Answers2

8

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')
drew010
  • 68,777
  • 11
  • 134
  • 162
2

->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);
Joel Lord
  • 2,175
  • 1
  • 18
  • 25