OR clause in Zend DB update?
Asked Answered
V

2

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
Verified answered 27/6, 2012 at 22:53 Comment(2)
possible duplicate of How to Use Multiple Conditions In An Update Statement With Zend_Db And QuoteIntoHatchery
@cbuckley This would combine the WHERE using AND which is not what the OP wants.Kilometer
K
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')
Kilometer answered 27/6, 2012 at 23:30 Comment(0)
M
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);
Mahau answered 28/6, 2012 at 12:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.