Here is the scenario: letâs say that we have a Blog, where at the end of each Article we can have an optional list of âRelated Articlesâ. The âRelated Articlesâ are implemented in the Panel as a TAGS field: the field lists other articlesâ titles, but stores their ID.
Now, whenever an existing article is DELETED from the site, we must do a check to see whether that article is listed as a ârelated articleâ in any article pages - and if so, that reference must be deleted, before the actual article itself is deleted - akin to a âcascade deleteâ on a database. So, how do we do this the most âelegant, Kirby 3â way? More specifically: do we use a hook or a model?
The obvious way to implement this would be to write a function in the 'page.delete:before'
hook. But that hook would trigger on the deletion of every page, not just article pages, so weâd have to start the function by doing a check - not very optimal.
The more âOOPâ-ish way to handle CRUD functions on data is to put all those functions in the model - e.g., create an ArticlePage
model class that extends the parentâs âdeleteâ method to add additional checking operations it will do on âdeleteâ. That âfeelsâ like the âproperâ way to do it, as the method would then only be called when Article pages are deleted - not all pages - and keeps all data-management operations inside the model. However, there is very little documentation in the Guide about extending the default CRUD functions, and even a warning about being careful when overriding the Page class - so Iâm not sure whether this is actually the best way to go about itâŚ
Any guidance from the Kirby Gods would be appreciated!