MODX Community

Updating User Settings

Should be a quick one for most modxers out there but i just haven’t been able to figure this one out.

I’ve got a user setting for each user named “active_survey_id” and i’m failing to be able to update this on form post in my snippet.

I’ve been messing around with various bits of codes i’ve been able to find on various sites. It’s actually been really difficult to get information regarding this. Should i be using something else to house settings that are changed for individual users?

Please put me out of my misery!

if(isset($_POST['idProject'])) {
    $postedSurvey = $_POST['idProject'];
    echo "$postedSurvey";
    //$profile = $modx->user->getOne('Profile');
    //$fields = $profile->get('extended');
    //$fields['active_survey_id'] = '10';
    //$profile->set('extended', $fields);
    $setting = $modx->getObject('active_survey_id');
    $setting->set('active_survey_id', $postedSurvey);
    //echo "$userSurvey";
    //$newId = $modx->getObject('active_survey_id');
    //$newId->set('value', '3');

I’ve checked out the following page and it didn’t seem to help me:
Modx User Settings

just to be clear hoping that the POST value ‘idProject’ can be used to update the value of the user setting ‘active_project_id’.

Hope it makes sense.

What’s the reason to store that in User Settings and not in Extended Fields?

No particular reason Bruno, i had thought that user>settings was the most elegant way to do this. However I can defo give Extended fields a crack. Not tried yet.

@bruno17 would i be any chance of one user altering the extended field of another user? The user making the change has an assigned ACL role higher than the other user. Not sure if that’s relevant or not.

Extended fields seem to work well for editing users own variable/field. Thanks for that.

if(isset($_POST[‘idProject’])) {
$postedSurvey = $_POST[‘idProject’];
echo “$postedSurvey”;

$profile = $modx->user->getOne('Profile');
$fields = $profile->get('extended');
$fields['active_survey_id'] = $postedSurvey;
$profile->set('extended', $fields);


To change user settings, you’ll need to first load or create the modUserSetting object:

$setting = $modx->getObject('modUserSetting', [
    'key' => 'active_survey_id',
    'user' => $modx->user->get('id')
if (!$setting) {
    $setting = $modx->newObject('modUserSetting');
    $setting->set('key', 'active_survey_id');
    $setting->set('user', $modx->user->get('id');

Then set the value and save the modUserSetting object:

$survey = $_POST['idProject'];
// @todo add validation for this value: allowed project? right format/value type? 
$setting->set('value', $survey);

// Save it to the database

// add it to the UserSettings on the user in memory
$modx->user->addMany($setting, 'UserSettings');

Finally if I recall correctly user settings take an extra step for them to apply in a current session, depending on how you’re using them, because there’s some caching going on in the session itself. I’ve not tested this, but think this should do the trick:

$modx->getUser('', true);

(That relies on having added the setting to the UserSettings in-memory representation as that wont look it up from the database if it has already been loaded before.)

Depending on how you use the setting in your site, redirecting the user (in other words, starting a new request) may be necessary to have that apply.

All in all, I agree with Bruno’s suggestion to use the extended profile field as that’s simpler and has less “gotchas”.

1 Like

Thanks @markh for spelling that out. What @bruno17 stated definitely works for me. I’m now required to update the extended field to another user (client) and can’t quite nail it.

I found this code at Bobs Guide’s

//Get active user active_survey_id extended field
$usr = $modx->user;
$profile = $usr->getOne(‘Profile’);
$fields = $profile->get(‘extended’);
$activeSurvey = $fields[‘active_survey_id’];
echo “
User active survey: $activeSurvey”;

//Get Client user active_survey_id extended field
$usr = $modx->getObject(‘modUser’,
array(‘name’ => ‘client’));
echo “
$profile = $usr->getOne(‘Profile’);
$fields = $profile->get(‘extended’);

The var_dump of $usr variable comes back “NULL” so the next line (#37) falls over.


User active survey: 3
Fatal error : Uncaught Error: Call to a member function getOne() on null in C:\Work\WWW\odysseus2\core\cache\includes\elements\modsnippet\13.include.cache.php:37 Stack trace: #0 C:\Work\WWW\odysseus2\core\model\modx\modscript.class.php(76): include() #1 C:\Work\WWW\odysseus2\core\model\modx\modparser.class.php(537): modScript->process(NULL) #2 C:\Work\WWW\odysseus2\core\model\modx\modparser.class.php(251): modParser->processTag(Array, true) #3 C:\Work\WWW\odysseus2\core\model\modx\modresource.class.php(511): modParser->processElementTags(’’, ‘…’, true, false, ‘[[’, ‘]]’, Array, 9) #4 C:\Work\WWW\odysseus2\core\model\modx\modresource.class.php(463): modResource->parseContent() #5 C:\Work\WWW\odysseus2\core\model\modx\modresponse.class.php(62): modResource->prepare() #6 C:\Work\WWW\odysseus2\core\model\modx\modrequest.class.php(138): modResponse->outputContent(Array) #7 C:\Work\WWW\odysseus2\core\model\modx\modrequest.class.php(122): modRequest->prepareResponse() #8 C:\Work\WWW\odysseus2\core\model\modx\modx in C:\Work\WWW\odysseus2\core\cache\includes\elements\modsnippet\13.include.cache.php on line 37

Got it guys. Using the user id worked. Many thanks for your time @markh and @bruno17!

//Get active user active_survey_id extended field
$usr = $modx->user;
$profile = $usr->getOne(‘Profile’);
$fields = $profile->get(‘extended’);
$activeSurvey = $fields[‘active_survey_id’];
echo “
User active survey: $activeSurvey”;

//Get Client user active_survey_id extended field
> $usr = $modx->getObject(‘modUser’,
> array(‘id’ => ‘4’));
echo “
$profile = $usr->getOne(‘Profile’);
$fields = $profile->get(‘extended’);
$clientActiveSurvey = $fields[‘active_survey_id’];

echo “
Client active survey: $clientActiveSurvey”;