class Plugin extends PluginBase
{
public function boot()
{
EntryRecord::inSection('Catalog\Order')::extend(function ($model) {
$model->bindEvent('model.beforeSave', function () use ($model) {
$model->add_status_to_history = 1;
});
});
}
But when I trying to save any other model, for example Catalog\Product
I got
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'add_status_to_history' in 'field list' (SQL: update .... `add_status_to_history` = 1,
But it’s different models Catalog\Order and Catalog\Product. It seems extend affects all models
Some sugar has been added for this in v3.1.15 onwards. Here is what we’ve added to the documentation about this:
Extending Model Constructor
Similar to extending regular models, you may extend the EntryRecord model constructor using the extendInSection method to target a specific blueprint.
EntryRecord::extendInSection('Blog\Post', function($model) {
$model->bindEvent('model.beforeSave', function () use ($model) {
// Model has been saved!
});
});
The extendInSectionUuid method can also be used for more precise targeting. This approach will not throw an exception if the blueprint is not found.
EntryRecord::extendInSectionUuid('3328c303-7989-462e-b866-27e7037ba275', function($model) {
$model->bindEvent('model.afterDelete', function () use ($model) {
// Model has been deleted!
});
});