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

1

7

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?

Bunsen answered 24/6, 2013 at 16:2 Comment(0)
H
7

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)

Hardy answered 26/6, 2013 at 7:50 Comment(1)
Thank you Tomek! do you by any chance know where I can find a good list of examples/documentation with regards to Zend/db queries? I find this to be somewhat lacking on the docs.Bunsen

© 2022 - 2024 — McMap. All rights reserved.