Search code examples
eventscakephp-3.0modelsjoin

CakePHP 3 - Event on Model->link(...)


I am working on an application management panel, with Users and Apps. I would like to log the changes (add / edit / delete) to both Apps and Users. This I realised using a custom Behavior, using the afterSave() and afterDelete() callbacks.

Note that Apps hasMany Users and Users hasMany Apps, with a apps_users join table. I created models for Apps and Users.

However, I would also like to log the links between Apps and Users (the permissions). When permissions are given, a link between a user and an app is created using Apps->Users->link($app, [$user]).

Question: How to attach an callback to a $...->link(...) call?

As far as I can tell there is not regular callback for it, like there is for save() and delete()


Solution

  • Browsing my own questions when I noticed this.

    The solution would be to create table objects for the link table and add afterSave and afterDelete events for those tables. A behaviour could be created that is attached to those link table objects to reuse the logic.