Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZF2 + Zend\Db\Sql\Update, adding to current value

I'm trying to do something relatively simple but can't figure it out.

I just want to add to a current value in the DB is there anyway to do the equivalent of a:

UPDATE `tablename` SET fieldB = fieldB + 1 WHERE fieldA='X'

Using the Zend/db update function?

like image 392
Juan Avatar asked Jun 24 '13 16:06

Juan


1 Answers

it will be something like this:

 $select = $sql->update();
 $select->table('basket');
 $select->set(['quantity' => new Expression("quantity + ? ", [$quantity])]);
 $select->where(['basket_id'=>$basket_id]);

Remember to escape/sanitize your data! (like i do with $quantity)

like image 79
Tomek Kobyliński Avatar answered Sep 29 '22 06:09

Tomek Kobyliński