CodeIgniter Forums
Using updateBatch correctly - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Using updateBatch correctly (/thread-77204.html)



Using updateBatch correctly - zjorge - 08-01-2020

I'm about to add this to my controller:

PHP Code:
foreach ($orders as $order) {
  $data['order_id'] = $order['id'];
  $data['status'] = $order['status'];
  $orderModel->save($data);


But I'm always concerned about this repeated calls to the DB ending on a syncing error or performance issue. Would this be the right moment to start using updateBatch?  If so, is this the way the controller would look like?

PHP Code:
foreach ($orders as $order) {
  $data[] = array(
  'order_id' => $order['id'],
  'status'=> $order['status']
  );
}
$result $orderModel->updateBatch($data,'order_id'); 

This is CI4 and Orders Model has no functions, only $allowedFields and $primaryKey defined.


RE: Using updateBatch correctly - jreklund - 08-01-2020

Looks okay to me.