Welcome Guest, Not a member yet? Register   Sign In
Model $afterDelete and propagation
#1

(This post was last modified: 11-29-2023, 11:56 AM by grimpirate.)

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?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB