This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

jfo
jfo

Hi,

I have a custom controller and form to list/manage backend users. I extended the backend user model to MyUser model and I can list all users and see user details. The problem is that I can't have my overridden version of base models being invoked, only the parent version of the methods are being called.

Here is my model:

<?php namespace My\Models;

use Model;
use Backend\Models\User as BackendUserModel;

/**
 * MyUser Model
 */

class MyUser extends BackendUserModel
{

    public function __construct(array $attributes = array())
    {
        parent::__construct($attributes);
    }

    public function beforeCreate()
    {
        dd("this beforeCreate not being called, parent beforeCreate is being called instead");
       //...
    }

    public function getGroupsOptions()
    {
        dd("this getGroupsOptions not being called, parent getGroupsOptions is being called instead");
       //...

    }
}

What is wrong here? What do I need to do in order to have my beforeCreate and getGroupsOptions methods to override the parent methods?

philipptempel
philipptempel

You have extended the backend user model in order to provide your own backend user data source? If this is the case it won't work the way you did it. Just extending the model does not make it "the model to use". October's backend gets its users from the assigned model. It isn't that easy to override the model like you did. You'd have to go deeper into the code to achieve what you want. I'd suggest, as this is probably much easier and less invasive, that you have a plugin that extends the backend user model such that you can override the beforeCreate() and getGroupsOptions() methods.

jfo
jfo

Hi @philipptempel, thanks for your reply!

This model MyModel is already part of a plugin that I created. If you're refering to the Plugin.php boot method, I'm also extending the model there:

BackendUserModel::extend(function($model) {
    $model->belongsTo['myRelation'] = ['RelationModel'];
});

The problem is that I don't want to extend the beforeCreate event when a user is created using thebackend/settings/administrators form. I just want the new event to be triggered when a record is created using my own MyModel in my own form.

Is this possible?

Would using bindEvent as below help on that or would it bind the event globally?

BackendUserModel::extend(function($model) {
    $model->bindEvent('model.beforeCreate', function() use ($model) {
        // ...
    });
});

1-3 of 3

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.