Model $afterDelete and propagation - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Model $afterDelete and propagation (/showthread.php?tid=88904) |
Model $afterDelete and propagation - grimpirate - 11-29-2023 My setup is as follows in terms of a simplified database structure: table1, table2, table3, table4, table5. table1, table2 and table3 are independent tables. table4 has foreign keys to the primary keys of table2 and table3. table5 has foreign keys to the primary keys of table1 and table4. In SQL in order to propagate deletions I would use ON DELETE CASCADE on said foreign keys so that if I delete something in table1, table2, or table3, the relevant entries are deleted from table4 and/or table5. In the models I am using $useSoftDeletes = true, to prevent any permanent data loss. Of course this means that the ON DELETE CASCADE statement is no longer applicable. In order to reproduce the behavior of this I'm using an $afterDelete callback in my models. In table1 and table4 the callback will subsequently delete rows in table5. In table2 and table3 the callback will delete rows in table4. Note that I am using the respective models of each table within the callback (i.e. table1model, table2model, etc.) in order to perform the deletion. My logic tells me that if I delete something in table2, then its callback should delete something in table4. Finally, because something was deleted in table4 its callback should then delete something in table5. This however, is not the case. In my database results, table5 is wholly unaffected. So why is the $afterDeletion callback/event in table4 not being triggered when its model is being used inside of the $afterDelete callback of table2? |