Trash icon indicating copy to clipboard operation
Trash copied to clipboard

Add cascadingEmptyTrash method…

Open frandss opened this issue 2 years ago • 3 comments

…and modify emptyTrash method to adapt to new changes, preserving backward compatibility. Add related info to readme.

frandss avatar Sep 12 '22 08:09 frandss

Besides deleting of associated records the use of deleteAll() in empty trash also causes the afterDelete callback not being run for the primary record. For e.g. if the primary record has a uploaded behavior too which is supposed to delete a file when the record is deleted then that won't happen.

So a better way to solve all problems would be to just use Table::delete(), which would ensure delete callbacks are run. So we can either have a behavior config or an argument deleteWithCallbacks for emptyTrash(). If it's enabled then we first find all the trashed records and simply pass the resultset to Table::deleteMany().

ADmad avatar Sep 14 '22 13:09 ADmad

For e.g. if the primary record has a uploaded behavior too which is supposed to delete a file when the record is deleted then that won't happen.`.

Just ran into this same issue today. Need to delete files only when a record has been hard-deleted.

An option to use callbacks would be great or an event like Model.afterHardDelete.

kyleweishaupt avatar Oct 21 '22 21:10 kyleweishaupt

If I understand correctly the topic, I set ->setCascadeCallbacks(true); when defining relations and just use Table::delete(), because without it the hard delete is just doing generic DELETE on database in turn behaviors can't track deletions because the app doesn't actually know what is deleted.

It would be nice to be able to set cascadeCallbacks to true by default for all relations (to avoid accidentally forgetting) so any and all related deletions trigger delete events.

kolorafa avatar Apr 29 '24 14:04 kolorafa