-
-
Notifications
You must be signed in to change notification settings - Fork 193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Model mutators not working using SettingsModel behavior #1047
Comments
Hi, As a workaround, to make the code cleaner, it seems that the Encryptable trait works fine. |
@goldmont could you provide an example of what you tried to do that didn't work? |
Hi, sure.
public function registerSettings()
{
return [
'settings' => [
'label' => 'test.archive::lang.models.settings.title',
'description' => 'test.archive::lang.models.settings.description',
'category' => 'test.archive::lang.menu.init',
'icon' => 'icon-gear',
'class' => Settings::class,
'order' => 500,
'keywords' => 'settings setup'
]
];
}
fields:
token:
label: 'test.archive::lang.models.general.token'
span: left
type: sensitive
<?php namespace Test\Archive\Models;
use Illuminate\Support\Facades\Crypt;
use Model;
use Winter\Storm\Database\Traits\Validation;
class Settings extends Model
{
use Validation;
public $implement = [\System\Behaviors\SettingsModel::class];
public $settingsCode = 'test_archive_settings';
public $settingsFields = 'fields.yaml';
public $settingsCacheTtl = 0;
public $rules = [];
public function getTokenAttribute()
{
$value = $this->attributes['token'] ?? '';
return !empty($value) ? Crypt::decrypt($value) : null;
}
public function setTokenAttribute($value): void
{
$this->attributes['token'] = Crypt::encrypt($value ?? '');
}
} The value set into |
@mjauvin any ideas? |
@LukeTowers most likely caused by the changes in priority made in this PR |
I tested above suspicion and looks like this is not the cause. Still investigating. |
I reverted back to winter 1.2.1 and the bavior is till the same, so this has either never worked properly or it's been broken for a long time. |
@goldmont can you show your mutator code ? |
It's like the one in the Mutators documentation: public function setFirstNameAttribute($value)
{
$this->attributes['first_name'] = Crypt::encrypt($value ?? '');
} I read the code you wrote. I think I have to return the value instead of assigning it. |
By the way I can confirm that the following code works to me too: public function setFirstNameAttribute($value)
{
return Crypt::encrypt($value ?? '');
} |
Brain cramp alert! I swapped |
This will be closed automatically when the PR is merged. |
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
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:
The text was updated successfully, but these errors were encountered: