How to perform batch updates?

How can I perform batch updates in CodeIgniter instead of running a query every time in the database?

+3
source share
4 answers

The active CodeIgniter write class has an insert_batch () method that does just that and takes care of shielding the data.

$data = array(
  array('name' => 'John', 'email' => 'john@email.com'),
  array('name' => 'Sue', 'email' => 'sue@email.com')
);

$this->db->insert_batch('my_table', $data);

http://codeigniter.com/user_guide/database/active_record.html

+1
source

Mysql can perform multiple updates or inserts. Usually in the Active Record template you insert one after the other, but for mass updates or attachments you can do this.

$sql = "INSERT INTO table (id,Col1,Col2) VALUES (1,1,1),(2,2,3),(3,9,3),(4,10,12) ON DUPLICATE KEY UPDATE Col1=VALUES(Col1),Col2=VALUES(Col2);";
$this->db->query($sql);
+3
source

Code Igniter (, ):

? insert_batch update_batch [Sep/2010]. , .

From: http://codeigniter.com/forums/viewthread/188416/#891199

+1
source

This question was about updates, but the accepted answer is for inserts.

Here's how you perform a batch upgrade:

$data = array(
   array(
      'title' => 'My title' ,
      'name' => 'My Name 2' ,
      'date' => 'My date 2'
   ),
   array(
      'title' => 'Another title' ,
      'name' => 'Another Name 2' ,
      'date' => 'Another date 2'
   )
);

$this->db->update_batch('mytable', $data, 'title'); 

This example is provided in the CodeIgniter user guide:

http://ellislab.com/codeigniter/user-guide/database/active_record.html#update

0
source

Source: https://habr.com/ru/post/1771252/


All Articles