Cascading Delete

Wish to see a specific feature/change in future releases? Feel free to post it here, and if it gets enough "likes", we'd definitely include it in future releases!
Post Reply
rpierce
Veteran Member
Posts: 255
Joined: 2018-11-26 13:55
Location: Washington State

Cascading Delete

Post by rpierce » 2022-08-01 14:05

It would be great if AppGini had the ability to cascade delete all children records when a parent record is deleted. Just a thought.

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Cascading Delete

Post by jsetzer » 2022-08-01 14:40

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.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

rpierce
Veteran Member
Posts: 255
Joined: 2018-11-26 13:55
Location: Washington State

Re: Cascading Delete

Post by rpierce » 2022-08-01 18:54

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.

rpierce
Veteran Member
Posts: 255
Joined: 2018-11-26 13:55
Location: Washington State

Re: Cascading Delete

Post by rpierce » 2023-11-28 17:37

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

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Cascading Delete

Post by jsetzer » 2023-11-28 19:29

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.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

Post Reply