Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework MySQL Update Column

Is there any way to do what the following code does, using Zend_Db_Table_Abstract?

UPDATE table SET value=value+1 WHERE value < 10;

I tried something like:

$tableModel->update(array('value=value+1'),'value<10');

but no success.

I could fetch the data in a SELECT and then just add 1 to that, but that's no option, cause it's very slow.

like image 261
Eduard Luca Avatar asked Sep 19 '11 20:09

Eduard Luca


1 Answers

The first argument array is an associative mapping of columns and values. If you're not using an absolute value, i.e. you want to use an expression or function, you need to use Zend_Db_Expr. The following should increment the 'value' column of any rows with a current value under 10.

$tableModel->update(array(
    'value' => new Zend_Db_Expr('value + 1')
), 'value < 10');
like image 189
David Snabel-Caunt Avatar answered Sep 19 '22 13:09

David Snabel-Caunt