Jump to content

Programatically update user's profile field


seanzol

Recommended Posts

I'm attempting to access and update a user's profile field from within a page, using the following code:

$member = \IPS\Member::loggedIn();
$fields = $member->contentProfileFields();
$fields['core_pfieldgroups_3'][3] = implode(',', $to_save);
$member->save();

However, these changes aren't actually committed back to the database. What am I doing wrong?

Link to comment
Share on other sites

Profilefields are not updateable through $member->save() as far as I know. 

Looking at how it's done in applications/core/modules/front/members/profile.php it seems you need to go about it this way: 

$member = \IPS\Member::loggedIn();
$fields['member_id'] = $member->member_id;

# Example 1 where you also make use of the previous value to set the new value in some way
$originalFields = $member->profileFields();
$fields['field_3'] = $originalFields['core_pfieldgroups_1']['core_pfield_3'] . " Append this text.";

# Example 2 where you simply update it with the implode for your $to_save-variable:
$fields['field_3'] = implode(',', $to_save);

\IPS\Db::i()->replace( 'core_pfields_content', $fields );

Obviously you'll need to identify the pfieldgroup and pfield id that is relevant for you. 

Link to comment
Share on other sites

  • 2 weeks later...
On 9/9/2015, 6:48:10, TSP said:

Profilefields are not updateable through $member->save() as far as I know. 

Looking at how it's done in applications/core/modules/front/members/profile.php it seems you need to go about it this way: 


$member = \IPS\Member::loggedIn();
$fields['member_id'] = $member->member_id;

# Example 1 where you also make use of the previous value to set the new value in some way
$originalFields = $member->profileFields();
$fields['field_3'] = $originalFields['core_pfieldgroups_1']['core_pfield_3'] . " Append this text.";

# Example 2 where you simply update it with the implode for your $to_save-variable:
$fields['field_3'] = implode(',', $to_save);

\IPS\Db::i()->replace( 'core_pfields_content', $fields );

Obviously you'll need to identify the pfieldgroup and pfield id that is relevant for you. 

Oops, forgot to hit notify me, thank you so much! I ended up doing it manually like I did in 3.4, but I'd like to switch over to the proper way haha

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...