XF 2.2 Custom user fields code wont update

Dannymh

Active member
Hi,

I think this may be due to permissions. but am trying to find a way around it.
I have a custom user field but the user field is not editable by end users only Admin and Moderator. However the field needs to be checked by code if certain conditions are met.
I run the code and everything else saves but the custom field does not.

At the moment the user field is a drop down with a yes/no value I run

PHP:
 $userEm = $app->em()->findOne('XF:User', ['user_id' => $json["vendorData"]]);
    $userEm->Profile->custom_fields->otbVerified = 'yes';
    $userEm->save();

However the user cant update it as the permissions are set in the custom field to be not editable by the user. Am I correct in thinking this is a restriction in the Entity due to that permission and is there possibly any other way that I may be able to update it from my code?
 
Try the following:
PHP:
$userEm = $app->em()->findOne('XF:User', ['user_id' => $json["vendorData"]]);
    $userEm->Profile->custom_fields->set('otbVerified', 'yes', 'admin');
    $userEm->Profile->save();

The third parameter in the set() method is instructing to save in 'admin' mode, which should overcome any permission hurdles.
 
Try the following:
PHP:
$userEm = $app->em()->findOne('XF:User', ['user_id' => $json["vendorData"]]);
    $userEm->Profile->custom_fields->set('otbVerified', 'yes', 'admin');
    $userEm->Profile->save();

The third parameter in the set() method is instructing to save in 'admin' mode, which should overcome any permission hurdles.
I'll give it a test this could be life changing and result in a slight code overhaul
 
Back
Top Bottom