ClassExtender: edit user profile

I’m struggling to find adequate documentation to show how to save data back to an existing user profile that has been extended with ClassExtender. BobRay’s documentation seems to reference new users, not how to deal with existing users.

I have a snippet that gets the profile of the current user:

$user = $modx->getUser(); $data = $modx->getObject('userData', array('userdata_id' => $user->get('id')); $fields = $data->toArray();

But later when I make my change, I’m not sure how to save changes back into $user->save();. What am I missing here?

Save should work. This is an example out of the docs: (https://docs.modx.com/revolution/2.x/administering-your-site/security/users)

`/* get the extended field named "color": */`

`$fields` `= ` `$profile` `->get(` `'extended'` `);`

`$color` `= ` `$fields` `[` `'color'` `];`

`/* set the color field to red */`

`$fields` `= ` `$profile` `->get(` `'extended'` `);`

`$fields` `[` `'color'` `] = ` `'red'` `;`

`$profile` `->set(` `'extended'` `,` `$fields` `);`

`$profile` `->save();`

You might have a look at the update profile snippet. It only gets the user profile object, but it could be customized…

ClassExtender creates a new table called: ext_user_data

MOX user details are stored in the modx_users table.

I think that the ext_user_data table creates a relationship with the modx_users* table via its userdata_id field.

ext_user_data
id: 1
userdata_id: 2

modx_users
id: 2

So I think that you need to check you database to ensure related records were created for existing users when ClassExtender was installed.

I do not have enought knowledge of php to answer your question, but I hope that this information may help you on your way.

@ilja-web I think that your example relates to the built in Extended Fields which are different from the Extende fields created using ClassExtender.

Found the fix - should have done this from the start, but I’m a newbie to ClassExtender. I took a look at BobRay’s ExtUserUpdateProfile snippet, and he has it all there.

For those watching at home, this is the code I started my snippet with that retrieved the extended data from the user profile:

$user = $modx->getUser(); 
$data = $modx->getObject('userData', array('userdata_id' => $user->get('id')); 
$fields = $data->toArray();

Normally I would run $user->save(); however, it’s $data that contains the extended info, and is attached to both the ext_user_data and modx_users tables. So where I was trying save to $user, that was the failure. So, the simple solution:

$data->set('key', $value);
$data->save();

Thanks for this package @bobray - very elegant solution :ok_hand:

Thanks for the kind words. I’m glad it worked for you. :slight_smile:

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.