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

<pre><code>$userModel-&gt;getContent()-&gt;setAttributes(array(
    'mycustomfield' =&gt; 'my field value'
));

if (!craft()-&gt;users-&gt;saveUser($userModel))
{
    $errors = $userModel-&gt;getErrors();
}
</code></pre>

CC BY-SA 3.0
2016-03-26T09:38:38.393Z
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
CC BY-SA 3.0
2016-03-26T21:05:14.887Z
Provided a working solution.
BillyMedia
CC BY-SA 3.0
2016-03-26T02:04:42.617Z
2016-03-26T02:04:42.617Z
Brad Bell
<p>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).</p>

<p>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.</p>

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

<pre><code>craft()-&gt;users-&gt;getUser();
</code></pre>

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

<p>Try this instead;</p>

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

CC BY-SA 3.0
2017-09-13T10:14:24.453Z
2017-09-13T10:14:24.453Z
foamcow
<p>I am using saveUser and i am trying to save a value to a user's custom field.</p>

<pre><code>        $userModel = new UserModel();
        $userModel-&gt;mycustomfield = 'test value';
        craft()-&gt;users-&gt;saveUser($userModel);
</code></pre>

<p>This is what i have so far.</p>

<p>I am getting this error:</p>

<pre><code>Internal Server Error
Property "Craft\UserModel.mycustomfield" is not defined.
</code></pre>

<p>How do i save to a custom user field?</p>

<p><strong>EDIT</strong></p>

<p>Also tried:</p>

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

<p>No error, doesnt save either.</p>

<p><strong>EDIT</strong></p>

<pre><code>$userModel-&gt;getContent()-&gt;mycustomfield = 'my value';
</code></pre>

<p>No error, doesnt save.</p>

<p><strong>SOLUTION</strong></p>

<pre><code>    $user = craft()-&gt;userSession-&gt;getUser();
    $userModel = new UserModel();
    $userModel-&gt;id = $user-&gt;id;
    $userModel-&gt;username = $user-&gt;username;
    $userModel-&gt;email = $user-&gt;email;
    $userModel-&gt;password = '';
    $userModel-&gt;getContent()-&gt;mycustomfield = 'my custom value';
    $success = craft()-&gt;users-&gt;saveUser($userModel);
    if (!craft()-&gt;users-&gt;saveUser($userModel))
    {
        $errors = $userModel-&gt;getErrors();
    }
</code></pre>

CC BY-SA 3.0
2017-09-13T10:15:41.920Z
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
<plugin-development><plugins><craft-pro>
Saving to user's custom field
# Saving to user's custom field

(Asked by: username_0 on 2016-03-25)

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();
    }

**Tags:** plugin-development, plugins, craft-pro
> **Comments:**
> **username_1**: 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.  

---

## 2 answers

### Answer by username_2 on 2016-03-26. Score: 1 | Accepted answer

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();
}

> **Comments:**
> **username_0**: 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.  
> **username_0**: Provided a working solution.  

---

### Answer by username_1 on 2017-09-13. Score: 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();
}


---

