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?
Asked
Active
Viewed 1.2k times
7

Moyed Ansari
- 8,436
- 2
- 36
- 57

Sandesh Sharma
- 1,064
- 5
- 16
- 32
-
http://stackoverflow.com/a/10372550/1239506 – Moyed Ansari Dec 24 '13 at 06:35
2 Answers
9
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

Moyed Ansari
- 8,436
- 2
- 36
- 57
0
For Codeigniter 3, you can use this library. This allows you to be able to supply an array of key-value pairs to be inserted into separate rows and will auto-update data if a duplicate key occurs in DB. https://github.com/amir-ranglerz/CodeIgniter-Insert-Batch-on-DUPLICATE-KEY-Update

Amir Hussain
- 139
- 9