Lorenzo/Audistash 3.2 - delete associated BelongsToMany

Hi

I tried installing the recommended version of deuromark, but Composer prevents it due to project incompatibilities. I’m using Lorenzo’s version 3.2 on Cakephp 4.4.

In an ArticleTags relationship, if I create an Article with some Tags, the audit_logs table is updated for both the Article create event and the Tag create events. If I delete the same Article, the audit_logs table only records the Article event, not the Tags. I tried to ‘bake’ the join table and applying the plugin, but it still hasn’t resolved the issue.

Have you tried withcascadeCallbacksset to true in your association definition? Without this, the deletions are done in a single delete call to the database, not separately on each entity, which is how the plugin logs the data.

Indeed, the same would apply current audit stash of mine as well.

Practical fix for that setup:

  • enable dependent => true and cascadeCallbacks => true on the relevant association
  • attach AuditLog to the junction table if you want ArticlesTags delete records logged
  • do not expect Tag delete logs unless you are actually deleting tags rows

CakePHP’s own delete docs line up with this: cascadeCallbacks is what switches from bulk deletes to per-entity deletes for dependent associations. Source: Deleting Data | CakePHP

Great, I followed your and Zuluru’s suggestions, and now it works as expected. When an article associated with N tags is deleted, the deletion of the corresponding records in the join table is also logged as an event in audit_logs. I’ll just point out that it was enough to add the ‘cascadeCallbacks’ clause alone.

ArticlesTable:

$this->belongsToMany(‘Tags’, [
‘foreignKey’ => ‘article_id’,
‘targetForeignKey’ => ‘tag_id’,
‘joinTable’ => ‘articles_tags’,
‘through’ => ‘ArticlesTags’,
‘cascadeCallbacks’ => true
]);

Many thanks to everyone.