Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete rows from different tables in CodeIgniter

A little bit of code-snippet:

$this->db->where('id', $outfit_id);
$this->db->update('outfit_main',$data);

//Delete productsettings for specific outfit (They are added below with new values)
$this->db->where('outfit_id', $outfit_id);
$this->db->delete('outfit_products');

//Delete outfit_images for specific outfit as well. They are also added below
$this->db->where('outfit_id', $outfit_id);
$this->db->delete('outfit_images'); 

What I want to do is:

  1. Delete rows from outfit_main where id = $outfit_id
  2. Delete rows from outfit_products where outfit_id = $outfit_id
  3. Delete rows from outfit_images where outfit_id = $outfit_id

But in fact when I add the last two rows:

$this->db->where('outfit_id', $outfit_id);
$this->db->delete('outfit_images'); 

*it deletes all rows from outfit_main as well. why?*

like image 883
bestprogrammerintheworld Avatar asked Jan 12 '23 05:01

bestprogrammerintheworld


2 Answers

Try combining the where clause with the delete active record:

$this->db->delete('outfit_main', array('id' => $outfit_id));
$this->db->delete('outfit_products', array('outfit_id' => $outfit_id));
$this->db->delete('outfit_images', array('outfit_id' => $outfit_id));
like image 67
doitlikejustin Avatar answered Jan 17 '23 23:01

doitlikejustin


it seems that, Ci is using old/cached query for where clauses, so if you flush to clear the cache using

$this->db->flush_cache();

And then use

$this->db->where('outfit_id', $outfit_id);
$this->db->delete('outfit_images');

Then, maybe it'll work in right way. Check caching examples, it'll be more clear and I've confusion about it as well, so waiting for response.

like image 35
The Alpha Avatar answered Jan 17 '23 21:01

The Alpha