I saw a removeAll method in ManyManyList, that also doesn’t delete the actual record.
I did it with:
# Get existing links
$Links = $Event->EventLinks();
# Use the result so it's actually there
$ids = [];
foreach ($Links as $Link) $ids[] = $Link->ID;
if ( ! empty($ids))
{
# Remove all the relations
$Event->EventLinks()->removeAll();
# Remove each EventLink record
foreach ($ids as $ID)
{
EventLink::delete_by_id('EventLink', $ID);
}
}
I’ll try that. I did read that but assumed it meant when deleting the “parent” record, delete the many relations. That’s not what I’m looking for. I just want to clear out the parents relations and add some more. Parent doesn’t get deleted.
Got you, sorry I misunderstood what you were asking. Yes, the cascade deletes work when you delete the “parent” record.
The reason it works the way it does is that in a many_many situation, the “child” records could be linked in a number of places, so removing the relation only is the safe way to work (else you could inadvertently be removing records which are still linked elsewhere).
To manually delete the associated records, you could probably simplify your code a little, by not needing the intermediate array:
foreach ($Event->EventLinks() as $Link) {
$Link->delete();
}
//$Event->EventLinks()->removeAll();
I did try that (amongst a grip of other things) and I think the removeAll() didn’t work when the children have already been deleted as it EventLinks() doesn’t have any results. I could be mistaken on that - will try.
Nah, it’ll remove the child Link records but leave the orphaned records in the many_many join table. You don’t ever see the orphans and they don’t have any effect on the system but no point polluting the table…
If I do the removeAll() before the $Link->delete, the joining table rows get deleted correctly but leaves the Link record. $Link->delete() has no effect.
The docs do say the function is remove() not delete so I tried that too. Same behaviour happens as delete.
One of the solutions would be to add an onAfterDelete() method to the ‘child’ object which removed the entries from the relation table. It’s not especially pretty, but it would work.