Welcome Guest, Not a member yet? Register   Sign In
Is it possible to delete data and relationship with CI's Active Record?
#1

[eluser]DominixZ[/eluser]
This is code i use.

Code:
$this->db->where('id',$video_id)->delete('video');
$this->db->where('video_id',$video_id)->delete('video_files');
$this->db->where('video_id',$video_id)->delete('third_party_users_relationship_with_video');

And I see CI has delete multi-table like this.

Code:
$tables = array('table1', 'table2', 'table3');
$this->db->where('id', '5');
$this->db->delete($tables);

Is it can be like this or something similar to this.
Code:
$video_id = 10;
$tables = array('video' => 'id' , 'video_files' => 'video_id' , 'third_party_users_relationship_with_video' => 'video_id');
$this->db->delete($tables,$video_id);

Thank in advance.
#2

[eluser]xwero[/eluser]
You can't do it because your are mixing the where part of the statement with defining the tables.

What the array of tables does is nothing more than looping through the tables with the same where and limit part. From the AR class :
Code:
elseif (is_array($table))
        {
            foreach($table as $single_table)
            {
                $this->delete($single_table, $where, $limit, FALSE);
            }

            $this->_reset_write();
            return;
        }
You could do
Code:
foreach(array('video' => 'id' , 'video_files' => 'video_id' , 'third_party_users_relationship_with_video' => 'video_id') as $table => $field){ $this->db->where($field,$video_id)->delete($table); }
Which saves you 20 characters but it's not very readable.
#3

[eluser]Michael Wales[/eluser]
Usually this sort of thing is handled by the database, with cascading deletes (if using MySQL InnoDB). Otherwise, you're just going to have to do it manually as xwero suggests.




Theme © iAndrew 2016 - Forum software by © MyBB