Trash icon indicating copy to clipboard operation
Trash copied to clipboard

BelongsToMany with enabled setCascadeCallbacks for in-beetween tables question

Open kolorafa opened this issue 1 year ago • 0 comments

Steps to reproduce it:

  1. Have Users belongsToMany Roles - both having Trash behavior
  2. Set the setCascadeCallbacks on that relation to true (it's false by default)
  3. The in-between table is intentionally set not to have Trash behavior
  4. Trigger trash on User that have related Roles
  5. Trigger restore on the same user

Expected behavior:

the user is restored with the same related roles

Actual behavior:

the user is restored but all related roles are gone

I think this issue is because BelongsToMany as a 'exception' has a in-between table that is handled as exception in multiple places by checking for junction() table.

And in protected function _isRecursable(Association $association, Table $table): bool we check if the target has trash behavior rather than the in-between table has trash behavior.

https://github.com/UseMuffin/Trash/blob/e540acfbb5838ae174264b95b8b0469c03bb99e8/src/Model/Behavior/TrashBehavior.php#L386

Is it correct to assume that we should check if the Association is BelongsToMany type and check if the related table support Trash?


Side note - this doesn't happen by default as setCascadeCallbacks is not enabled by default on BelongsToMany, we intentionally set it to true to have delete Audits, as without it it's doing a DELETE without knowing what it is actually deleting.

kolorafa avatar Apr 16 '24 06:04 kolorafa