<![CDATA[ Add extended fields to multiple existing user accounts - My Forums]]> https://forums.modx.com/thread/?thread=44901 <![CDATA[Re: Add extended fields to multiple existing user accounts]]> https://forums.modx.com/thread/44901/add-extended-fields-to-multiple-existing-user-accounts#dis-post-520754
How to set extended fields with containers? This doesn't work...
    $extended = $profile->get('extended');
    $extended['subscriptions'][$product_code] = $date;
    $profile->set('extended', $extended);
    $profile->save();


I dunno. After fussing with it for a while, the exact same code now works.]]>
sottwell Feb 17, 2015, 01:10 AM https://forums.modx.com/thread/44901/add-extended-fields-to-multiple-existing-user-accounts#dis-post-520754
<![CDATA[Re: Add extended fields to multiple existing user accounts]]> https://forums.modx.com/thread/44901/add-extended-fields-to-multiple-existing-user-accounts#dis-post-258232
MODX will convert them for you if you put them there with xPDO, but a query would need them in the proper format.

The snippet version would look something like this:

<?php
$users = $modx->getCollection('modUser', array ('active' => 1));

foreach ($users as $user) {

    $profile = $user->getOne('Profile');

    $extended = array();

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

    $extended[] 'field1' => 'value1';
    $extended[] 'field2' => 'value2';

    $profile->set('extended', $extended);
    $profile->save();
}


The second argument to getCollection() is optional. Leave it out if you want to set the fields for all users, active or not.]]>
BobRay Jul 06, 2011, 12:52 AM https://forums.modx.com/thread/44901/add-extended-fields-to-multiple-existing-user-accounts#dis-post-258232
<![CDATA[Re: Add extended fields to multiple existing user accounts]]> https://forums.modx.com/thread/44901/add-extended-fields-to-multiple-existing-user-accounts#dis-post-258231 andrewhl Jul 05, 2011, 10:35 AM https://forums.modx.com/thread/44901/add-extended-fields-to-multiple-existing-user-accounts#dis-post-258231 <![CDATA[Re: Add extended fields to multiple existing user accounts]]> https://forums.modx.com/thread/44901/add-extended-fields-to-multiple-existing-user-accounts#dis-post-258230
The fields I am adding are for administrative purposes and need to be applied to user accounts, but can’t be visible/editable by them. They’re basically hidden boolean fields set to true/false by administrators for each user.

The fields should not contain any values when set initially. I could add them to the update profile chunk, but I don’t want them visible to users (who /are/ permitted to update other fields in their profile).

I need to add them to all users. I’ve already added some manually, I just need to finish the rest. I could write a SQL query to do this, but if there’s an easier way, I’d love to hear it.

Thanks!]]>
andrewhl Jul 05, 2011, 01:35 AM https://forums.modx.com/thread/44901/add-extended-fields-to-multiple-existing-user-accounts#dis-post-258230
<![CDATA[Re: Add extended fields to multiple existing user accounts]]> https://forums.modx.com/thread/44901/add-extended-fields-to-multiple-existing-user-accounts#dis-post-258229
Do you want to set a value for any of the new fields, and if so, will it be the same for each user our not?]]>
BobRay Jun 28, 2011, 09:41 AM https://forums.modx.com/thread/44901/add-extended-fields-to-multiple-existing-user-accounts#dis-post-258229
<![CDATA[Re: Add extended fields to multiple existing user accounts]]> https://forums.modx.com/thread/44901/add-extended-fields-to-multiple-existing-user-accounts#dis-post-258228
Or do you want to create the field and save the value at the same time? Then you might hack the database directly.

The extended fields are in a column called ’extended’ in the table [yourprefix]_user_attributes. Extended fields are saved in one field in a array-like format, like so:
{"firstname":"Bas","lastname":"Zijlstra"}


You already have extended fields, so you need to update the values rather than overwrite them. Do something like:

update [yourprefix]_user_attributes set extended = concat(left([yourprefix]_user_attributes.extended, length([yourprefix]_user_attributes.extended) - 1), ',"foo":"bar"', '}') where id = [n]


There may be a simpler way. Also, I’d recommend that you back up your data first!]]>
drdoolittle Jun 28, 2011, 08:42 AM https://forums.modx.com/thread/44901/add-extended-fields-to-multiple-existing-user-accounts#dis-post-258228
<![CDATA[Add extended fields to multiple existing user accounts]]> https://forums.modx.com/thread/44901/add-extended-fields-to-multiple-existing-user-accounts#dis-post-258227
I’m already using the Register snippet to create various custom fields on signup; now, however, I need to go back and add a number of new fields to the existing users. There are far too many users to do this manually, i.e., through the MODx Manager.

Any suggestions?]]>
andrewhl Jun 23, 2011, 01:42 AM https://forums.modx.com/thread/44901/add-extended-fields-to-multiple-existing-user-accounts#dis-post-258227