wintercms / wintercms/winter

Model mutators not working using SettingsModel behavior

Open
#1,047 16 comments 0 reactions 1 assignee View on GitHub

@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
  1. Create a Settings model.
  2. Implement a mutator and return a different value for that form field.
  3. 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.