Page 1 of 1

Cascading Delete

Posted: 2022-08-01 14:05
by rpierce
It would be great if AppGini had the ability to cascade delete all children records when a parent record is deleted. Just a thought.

Re: Cascading Delete

Posted: 2022-08-01 14:40
by jsetzer
Hmmm, should work.

Can you check the following settings:
  1. Grant DELETE-permission on the subtables for the user's group who is deleting the master record
  2. Check the correct setup of the lookups (as those connect a subtable's records to a master record)
  3. Option [x] Delete records even if they have children records
  4. Option [X] Allow mass delete on the master table
  5. Just for clarification: Check the PHP function TABLENAME_delete() in TABLENAME_dml.php. All related subtables should have a separate block of code, starting with // child table: SUBTABLENAME. If your code does not delete related records of certain tables, check if those tables do have a separate code-block there. If not, I guess there is a problem with setup of that specific lookup.
If all of this does not enable cascading-delete, you can still delete in _before_delete()-hook. After deleting all child-records manually you should also cleanup orphanened membership_userrecords.

PS: Another idea is creating a trigger on database level, but, personally, I prefer not creating database triggers for better (=easier) portability/migration/updates.

Re: Cascading Delete

Posted: 2022-08-01 18:54
by rpierce
Hi Jan,

I have done the items you suggest and am able to delete the parent record but then I still have orphaned children records.

Re: Cascading Delete

Posted: 2023-11-28 17:37
by rpierce
Hi Jan,

How do I write that query to delete child records in the before delete hook? I have things set the way you describe in your post, however I still get orphaned records whenever the parent record is deleted.

Ray

Re: Cascading Delete

Posted: 2023-11-28 19:29
by jsetzer
As starting point:

Given two tables A, B with B.a_id = A.id

in A_after_delete-hook you should get primary key of deleted record in $selectedID variable.

Code: Select all

$cmd = "DELETE FROM B WHERE B.a_id = '{$selectedID}'";
$eo = [];
sql($cmd, $eo);
PS: You should cleanup membership_userrecords, too.