Welcome Guest, Not a member yet? Register   Sign In
need a little guidance on a recursive db delete
#1

[eluser]dan.syme[/eluser]
Having troubles visualizing what needs to happen here, this has been my thought process so far
1. create a query to find all records in 4 tables that are associated with each other through id and parent_id's.
2. run the result through a foreach and delete each record in the tables.
3. stuck on the correct way to delete the records from the table.

Code:
$this->db->select('group1.id, group2.id, group2.parent_id, group3.id, group3.parent_id, group4.id, group4.parent_id');
            $this->db->from('group1');
            $this->db->join('group2', 'group2.parent_id = group1.id');
            $this->db->join('group3', 'group3.parent_id = group2.id');
            $this->db->join('group4', 'group4.parent_id = group3.id');            
            $this->db->where('group1.id', $id);
            
            $query = $this->db->get();
            
            $tables = array('group1', 'group2', 'group3', 'group4');
            
                       // this is where I am having an issue. I need to delete from each table based on the query response.
            foreach ($query->result() as $row) {
                
                // I need to iterate through the records, check its id, and delete it from its associated table.
                                $this->db->where('id', $row->id);
                $this->db->delete($tables);
                
            }

Thanks,
#2

[eluser]dan.syme[/eluser]
duh <- im a idiot

there is no need to run the result through a loop. I aleady built my delete string so all i need to do is

$this->db->delete();
#3

[eluser]dan.syme[/eluser]
spoke to soon, still cannot delete from all tables.

error: Unknown column 'group1.id' in 'where clause'

DELETE FROM `group2` WHERE `group1`.`id` = '10'

using the following code.

Code:
$tables = array('group1', 'group2', 'group3', 'group4');
            
$this->db->select('group1.id, group2.id, group2.parent_id, group3.id, group3.parent_id, group4.id, group4.parent_id');
$this->db->from('group1');
$this->db->join('group2', 'group2.parent_id = group1.id');
$this->db->join('group3', 'group3.parent_id = group2.id');
$this->db->join('group4', 'group4.parent_id = group3.id');            
$this->db->where('group1.id', $id);
$this->db->delete($tables);
#4

[eluser]dan.syme[/eluser]
its an issue discussed before, found a work around at this thread. The fix requires hacking the core.

http://ellislab.com/forums/viewthread/63991/
#5

[eluser]maria clara[/eluser]
got the same problem.. has anyone have solved this?




Theme © iAndrew 2016 - Forum software by © MyBB