Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

INSERT Batch, and if duplicate key Update in codeigniter

Is there any way of performing in batch Insert query and if the key already exists, UPDATE that row in codeigniter? I have gone through the documentation and found only insert_batch and update_batch. But how to update the row with duplicate key in active records? And what happens if one row fails to be inserted or updated in batch_insert? All insertion fails or only that row?

like image 707
Sandesh Sharma Avatar asked Dec 24 '13 06:12

Sandesh Sharma


1 Answers

You will have to go with little custom query by adding "ON DUPLICATE" statement

$sql = $this->db->insert_string('YourTable', $data) . ' ON DUPLICATE KEY UPDATE duplicate=duplicate+1';
$this->db->query($sql);
$id = $this->db->insert_id();

Also please check this out, it will give you better solution

like image 73
Moyed Ansari Avatar answered Oct 01 '22 10:10

Moyed Ansari