CodeIgniter Forums
Issue with updateBatch method - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Issue with updateBatch method (/showthread.php?tid=88749)



Issue with updateBatch method - ashlythomas - 10-30-2023

Hi,
I have a DB table named 'products'. It has the following columns:  id, stock_shipped, stock_on_hand
I am facing some issues in using the updateBatch method.
When an order is placed, the stock should update properly for all the items/products in the order.
So I need to increase/decrease the existing value in DB table columns: stock_shipped and stock_on_hand

Code:

PHP Code:
$data[0]['id'] = 100;
$data[0]['stock_shipped'] = stock_shipped 10;
$data[0]['stock_on_hand'] = stock_on_hand 10;

$data[1]['id'] = 101;
$data[1]['stock_shipped'] = stock_shipped 25;
$data[1]['stock_on_hand'] = stock_on_hand 25;
...
...
...


$builder $this->db->table('products');
$builder->updateBatch($data'id'); 


Can you please suggest a way to use the same method: updateBatch correctly?


RE: Issue with updateBatch method - luckmoshy - 10-30-2023

Can you carefully read here please https://codeigniter.com/user_guide/database/query_builder.html?highlight=batch#updatebatch


RE: Issue with updateBatch method - ashlythomas - 10-31-2023

I read the documentation. Nothing was mentioned about the existing value. 
How can I add/subtract from, the current value of a database column when I use the data array?


RE: Issue with updateBatch method - kenjis - 10-31-2023

(10-31-2023, 12:41 AM)ashlythomas Wrote: How can I add/subtract from, the current value of a database column when I use the data array?

I'm not sure it works, but try to use RawSql.
See https://codeigniter.com/user_guide/database/query_builder.html#builder-insert