0

I try to make universal update / swap which would always switch active status. In SQL is simple:

UPDATE contacts SET active = ABS( active - 1 ) WHERE id = ....

Where active is small int / flag 0 or 1, so it always work.

When I try to implement it to model class extend Zend_Db_Table:

public function disableContact( $contact_id )
{
    $where = $this->getAdapter( )->quoteInto( 'id = ?', $contact_id );
    $data = Array( );
    $data ['active'] = '(ABS( the.contacts.active - 1 ))';

    return parent::update( $data, $where );
}

I got Error: SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: "(ABS( the.contacts.active - 1 ))"<p><pre>#0 ......./ZendFramework-1.11.0/library/Zend/Db/Statement.php(300): Zend_Db_Statement_Pdo->_execute(Array)

How to pass ABS expression to update ?

bensiu
  • 24,660
  • 56
  • 77
  • 117

1 Answers1

3

Use a Zend_Db_Expr, eg

// Updated with zerkms' much simpler bit flipping logic
$data['active'] = new Zend_Db_Expr('1 - the.contacts.active');

The best example I can find in the manual is for inserting data but the concept is the same. See http://framework.zend.com/manual/en/zend.db.table.html#zend.db.table.insert

FYI Only Zend_Db_Select recognises values wrapped in parentheses and treats them as expressions.

Phil
  • 157,677
  • 23
  • 242
  • 245