Hi,
Modx 2.7.3, I have a profile page with custom user settings (speed, difficulty, max_attempts, and more…)
On the profile page, the user can edit and save his settings.
I noticed the updated values are shown when the page reloads, however only when the user has just logged in and the user did not update his settings more than once after logging-in.
In other words:
IT WORKS WHEN: the user logs in, goes to his profile page and updates his settings —> the script saves the values and the page reloads with the updated values
IT DOESN’T WORK WHEN: the user had already updated his profile and tries to update it a second time — > the page reloads with the former values, a refresh is needed to see the updated values
I tried [[++difficulty]] or [[!++difficulty]]
Here is the relevant code, which is included in a foreach loop because there are several custom user settings to save:
foreach ( $fields as $key => $value ) {
$setting = $modx->getObject( 'modUserSetting', [
'key' => $key,
'user' => USERID,
]);
if ( !$setting ) {
$setting = $modx->newObject( 'modUserSetting' );
$setting->set( 'key', $key );
$setting->set( 'user', USERID );
}
$setting->set( 'value', (string)$_POST[ $key ] );
if ( $setting->save() ) {
//$modx->cacheManager->clearCache(); // THIS LINE DOESN'T HELP
//$modx->cacheManager->refresh(); // THIS LINE DOESN'T HELP
$modx->user->addMany( $setting, 'UserSettings' );
$modx->getUser( '', true );
}
}
I have tried several options to have the updated values showing up after the user has saved but it didn’t work.
Maybe is there another approach? Am I doing something wrong? Should I store all the new values in an array and only execute $setting->save() after the foreach loop is finished? Are there other solutions to have the correct values after the refresh?
Thank you very much for the help, all advice is greatly appreciated