OctoberCMS: Extending plugins using nullable trait

574 views Asked by At

How should traits be added to an existing model while extending plugins?

I tried the following with RainLab.User plugin:

UserModel::extend(function($model) {
    $model->implement[] = 'October.Rain.Database.Trait.Nullable';
    $model->nullable[] = 'company';
});

And got this error:

Indirect modification of overloaded property RainLab\User\Models\User::$nullable has no effect

1

There are 1 answers

0
LukeTowers On

You can't dynamically extend a class using Traits. You would have to use Behaviors instead, or (depending on what you need to do with your extended model) extend the model class with your own custom model class and then add the Trait to that custom model.

Easiest way would probably be to implement the Nullable Trait as a custom Behavior within your plugin, and then extend the model class with your Nullable Behavior.

If you'd like, you could also submit a feature request (via issues) to the repository asking for somebody to implement the database traits as behaviors to simplify similar extensions in the future.