Bulk insert update and delete data in codeigniter

Bulk insert update and delete data in codeigniter

Why you need to use the bulk insert, update, and delete in Codeigniter….? there are many reasons the one reason is you are fetching the id of a table and inserting that id in a new table.

If you are calling insert query in the loop, yes, you can do that, but it can decrease your system performance, so how can you insert the values..? Codeigniter proving the bulk adding functionality using query builder, you just need to call the method in your model $this->db->insert_batch().

Just imagine if you have millions of records inside your loop that means you are calling millions of time the insert query, this is not a good practice if you are working on MVC Framework.

foreach ($afterTripQuery as $sendAfter) { 
    $this->db->insert('tableName',array('columnName',$sendAfter['columnName'])) 
}

Here is the simple solution to your problem, as you can see below you are inserting many records using a single query, so it depends on you how you are coding in your project.

$arrayBatch =  array(); 
foreach ($afterTripQuery as $sendAfter) {
     $arrayBatch[] = array('columnName'=>$sendAfter['columnName']);  
} 
$this->db->insert_batch('tableName', $arrayBatch);

There are many ways where you can use the Batch insert and update in. CodeIgniter for supposing you are creating the blog system where you have a relational database like blogs table and the tags table, in the tags table you are sending the blog’s id as a foreign key in the tags table.

You can send the data of the tags in the array from HTML, now break the operation to inserting your blog.

Step 1:  insert the blog content in the blogs table and get the currently added id of the table and send that id to the tags table to enter the tag’s elements below the example code.

Here is the model’s method. We are inserting the blog content and fetching the currently added id.

public function addblog($value) { 
    $this->db->insert('blogs',$value); 
    return $this->db->insert_id(); 
}

Now we can fetch the tag’s data which is coming from the HTML

$data['b_slug'] = $this->input->post('blog_slug',true);//fetching the data from the  html 
$blogtags = array(); 
foreach ($tags as $tag) {    
    $blogtags[] = array(
    'blog_id'=>$blogId,//this is current blog id       
    'tag_name'=>$tag,       
    'tag_created'=> date("Y-m-d h:i:sa")    
    ); 
} 
$this->db->insert_batch('tags', $arrayBatch);

If you want to update the bulk records in CodeIgniter its very simple just use the update_batch from query builder  $this->db->update_batch(‘mytable’, $data, ‘columnName’);

$blogtags = array(); 
foreach ($tags as $tag) {    
    $blogtags[] = array(       
    'blog_id'=>$blogId,//this is current blog id       
    'tag_name'=>$tag,    
    ); 
} 
$this->db->update_batch('tags', $arrayBatch,'blog_id');//pass the columnName in the third parameter

So now ho can you delete your multiple records with a single query Its very simple just use the where_in method in query builder  $this->db->where_in(‘columName’,array()).

$blogtags = array(); 
foreach ($tags as $tag) {    
    $blogtags[] = $tag['tagId'];    
); 
} 
public function deleteTags($blogtags) {
  $this->db->where_in('tagId', $data);  
  $this->db->delete('tags'); 
}
shakzee
administrator
My Name is Shehzad Ahmed and I am a full-time developer with over 7+ years of experience. In that time, I’ve worked as a developer with many big and small firms and was teaching web design and development skills to many keen learners and by the time passes I’ve found a great passion and potential on myself as a teacher on this particular field and now I am a full-time teacher and working as a freelancer.

Leave a Reply

Your email address will not be published. Required fields are marked *

Featured Courses

Learn Web Development and web designing in few months from shakzee with real-world experience. Get up to 80% discount!