Search code examples
phpoctobercmsoctobercms-pluginsoctobercms-backend

October CMS plugin models relations


I am using a messages plugin by Autumn. https://github.com/gpasztor87/oc-messages-plugin It has 3 models in it (message, thread and participant) and now I want to create a separate section for this plugin in backend. I created a plugin using builder, I made a new model called "message", now the thing is that when I delete a message entry, I need it to delete other table entries associated to this message. Database is related like: thread has many messages and many participants.

The question is: should I create all 3 separate models in my new backend plugin or I should use the already created ones from the original plugin? Also, is it better to make a relation using October CMS or I could create some function onDelete() and just delete all related entries? If so, how could I make this function? Is there some thing like onDelete hook or something?

Thank you.


Solution

  • I managed to solve this by creating all three models in my new plugin and just adding relations on each one of them like this:

    public $hasMany = [
        'messages' => [
            'Autumn\Messages\Models\Message',
            'delete' => true
        ],
        'participants' => [
            'Autumn\Messages\Models\Participant',
            'delete' => true
        ]
    ];