winter: Model mutators not working using SettingsModel behavior
Winter CMS Build
dev-develop
PHP Version
8.1
Database engine
MySQL/MariaDB
Plugins installed
No response
Issue description
Hi,
The latest version of WinterCMS is affected by an issue that prevents values returned by mutators to be persisted to the database. I read the SettingsModel
behavior code and it seems that model $attributes
are mistakenly overwritten by “original” values.
Steps to replicate
- Create a Settings model.
- Implement a mutator and return a different value for that form field.
- Read the persisted value.
Workaround
Apparently the only solution is to override the beforeSave()
method inside the settings model and to manipulate the JSON contained into $this->attributes['value']
by first deserializing it and then serializing it back.
Example:
protected function beforeSave()
{
$fields = json_decode($this->attributes['value'], true);
$fields['token'] = Crypt::encrypt($fields['token'] ?? '');
$this->attributes['value'] = json_encode($fields);
}
About this issue
- Original URL
- State: open
- Created 5 months ago
- Comments: 16 (10 by maintainers)
This will be closed automatically when the PR is merged.
Brain cramp alert!
I swapped
mutator
withaccessor
in my mind… ignore everything I said and did so far… 🙄