2

I am using saveUser and i am trying to save a value to a user's custom field.

        $userModel = new UserModel();
        $userModel->mycustomfield = 'test value';
        craft()->users->saveUser($userModel);

This is what i have so far.

I am getting this error:

Internal Server Error
Property "Craft\UserModel.mycustomfield" is not defined.

How do i save to a custom user field?

EDIT

Also tried:

        $user = craft()->userSession->getUser();
        $userModel = new UserModel();
        $userModel->id = $user->id;
        $userModel->getContent()->setAttributes(array(
            'mycustomfield' => 'my field value'
        ));
        craft()->users->saveUser($userModel);

No error, doesnt save either.

EDIT

$userModel->getContent()->mycustomfield = 'my value';

No error, doesnt save.

SOLUTION

    $user = craft()->userSession->getUser();
    $userModel = new UserModel();
    $userModel->id = $user->id;
    $userModel->username = $user->username;
    $userModel->email = $user->email;
    $userModel->password = '';
    $userModel->getContent()->mycustomfield = 'my custom value';
    $success = craft()->users->saveUser($userModel);
    if (!craft()->users->saveUser($userModel))
    {
        $errors = $userModel->getErrors();
    }
  • I'm afraid, the solution you have come up with is flawed. You do not need to create a new UserModel. I have posted a solution below. – foamcow Sep 13 '17 at 10:15
1

It's probably a validation error. Use this to check if there are any.

$userModel->getContent()->setAttributes(array(
    'mycustomfield' => 'my field value'
));

if (!craft()->users->saveUser($userModel))
{
    $errors = $userModel->getErrors();
}
  • Ah, yeah had errors. Username cannot be blank Email cannot be blank How do i update a user, this to me suggests that i want to create one. I need it to update the logged in user. – BillyMedia Mar 26 '16 at 9:38
  • Provided a working solution. – BillyMedia Mar 26 '16 at 21:05
1

If you create a new UserModel then you will, as you have found, also need to set the username, password etc. as you effectively creating a blank model then filling it in and saving over the existing user's details (since you set the same ID).

This runs the risk of overwriting other custom data, resetting the password, removing group permissions etc. If ever you add a new custom profile field, you must also remember to update your code.

But you don't need to create a new UserModel as you are already making one when you use;

craft()->users->getUser();

This returns a UserModel, already populated with the user's details. So you only need to update the custom field.

Try this instead;

$user = craft()->userSession->getUser();
$user->getContent()->mycustomfield = 'my custom value';
$success = craft()->users->saveUser($user);
if (!craft()->users->saveUser($user))
{
    $errors = $user->getErrors();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.