Modifying Settings during BeforeSave

I’m trying to modify a setting during the beforeSave with code that previously worked in October 2 but since updating to October 3 it no longer works.

The code:

Settings::extend(function ($model) {
  $model->bindEvent('model.beforeSave', function () use ($model) {
    $locations = $model->getSettingsValue('locations');

    //code that modifies $locations

    $model->setSettingsValue('locations', $locations);
  });
});

The issue is that when you try to save the Settings model October runs an sql query like this:

update TABLE_NAME set value = 'all settings (including locations) go here, locations = ‘only locations data goes here’

That sql query fails because the locations column doesn’t exist, which it shouldn’t as far as I’m aware.

Is there some other command I’m supposed to use here instead of setSettingsValue or is no longer possible to update Settings during the beforeSave?

Hey @zimple.digital

I think this was refactored and streamlined in v3… does the following work?

Settings::extend(function ($model) {
  $model->bindEvent('model.beforeSave', function () use ($model) {
    $locations = $model->locations;

    //code that modifies $locations

    $model->locations = $locations;
  });
});

Also, make sure you are extending the SettingModel and not using the behavior…

Thanks that worked.

We were implementing the SettingsModel behaviour and extending Model instead of extending SettingModel.

1 Like