Model mutators not working using SettingsModel behavior
Open
@mjauvin is already working on this.
Since Feb 27, 2024.
- Dominant language
- PHP
- Stars
- 1.5k
- Forks
- 246
- Avg merge
- 19h 2m
- Merged PRs (30d)
- 7
Description
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);
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.