My Cart 0

Bulk insert update and delete data in codeigniter

Why you need to add bulk data 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 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 inserting 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 its totally depend 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.

Definitely, 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 inserted id of the table and send that id to the tags table to insert the tag’s elements below the example code.

Here is model’s method we are inserting the blog content and fetching the currently inserted 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
$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
I have been in the web design and development sector for over 5 years. I always provide high-quality courses which can be useful and practical for students, I am working for my community since 6 years and I have created thousands of lectures related to web development and different programming languages. Before selecting any course please make sure the language of the course because I am providing many courses in URDU and English language.

Leave a Reply

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

WhatsApp chat