<p>As of Craft 3.7.21, this can be done as follows.</p>
<pre class="lang-php prettyprint-override"><code>$user-&gt;name = 'New Name';
Craft::$app-&gt;elements-&gt;saveElement($user, false);
</code></pre>
<p>Prior to Craft 3.7.21, it was necessary to call <code>setFieldValue</code>.</p>
<pre class="lang-php prettyprint-override"><code>$user-&gt;setFieldValue('name', 'New Name');
Craft::$app-&gt;elements-&gt;saveElement($user, false);
</code></pre>
<p>This assumes that <code>$user</code> is a User element and that <code>name</code> is a custom field on users.</p>

CC BY-SA 4.0
2021-12-15T13:11:56.040Z
That's exactly what I needed. Thank you so much. As a matter of fact using elements->saveElement does the job exactly as if it was a "regular" entry element... I was searching too far ! :D Thanks again.
Sylvain Tremblay
CC BY-SA 4.0
2021-12-14T22:50:48.607Z
2021-12-14T22:50:48.607Z
Ben Croker
<p>I'm just trying to change a value of any field in the User Table in Craft 3.</p>
<p>Basically, say I retrieve a User by its ID and change the name and SAVE it...</p>
<pre><code>$oneUser = Craft::$app-&gt;users-&gt;getUserById($user_id);
if ($oneUser){
    $oneUser-&gt;name = &quot;robert&quot;;
}
//  $success = Craft::$app-&gt;users-&gt;saveUser($oneUser);
// $success = \craft\elements\User::saveElement($oneUser);
//Craft::$app-&gt;users-&gt;saveElement($oneUser);
// \craft\elements\User::saveElement($user_id);
</code></pre>
<p>As you can see, I tried many different ways, but I can't manage to properly make it to work.</p>
<p>In other regular Entry section, I managed to make it to work like this:</p>
<pre><code>   $thisOrder-&gt;setFieldValue('orderstatus', 'approved');
   $success = Craft::$app-&gt;elements-&gt;saveElement($thisOrder, false);
</code></pre>
<p>But I don't find the equivalent saveElement in User type...
Any help would be appreciated.</p>

<users><user-profile>
Backend Save user fields
# Backend Save user fields

(Asked by: username_0 on 2021-12-14)

I'm just trying to change a value of any field in the User Table in Craft 3.
Basically, say I retrieve a User by its ID and change the name and SAVE it...
$oneUser = Craft::$app->users->getUserById($user_id);
if ($oneUser){
    $oneUser->name = "robert";
}
//  $success = Craft::$app->users->saveUser($oneUser);
// $success = \craft\elements\User::saveElement($oneUser);
//Craft::$app->users->saveElement($oneUser);
// \craft\elements\User::saveElement($user_id);

As you can see, I tried many different ways, but I can't manage to properly make it to work.
In other regular Entry section, I managed to make it to work like this:
   $thisOrder->setFieldValue('orderstatus', 'approved');
   $success = Craft::$app->elements->saveElement($thisOrder, false);

But I don't find the equivalent saveElement in User type...
Any help would be appreciated.

**Tags:** users, user-profile

---

## 1 answers

### Answer by username_1 on 2021-12-14. Score: 2 | Accepted answer

As of Craft 3.7.21, this can be done as follows.
$user->name = 'New Name';
Craft::$app->elements->saveElement($user, false);

Prior to Craft 3.7.21, it was necessary to call setFieldValue.
$user->setFieldValue('name', 'New Name');
Craft::$app->elements->saveElement($user, false);

This assumes that $user is a User element and that name is a custom field on users.

> **Comments:**
> **username_0**: That's exactly what I needed. Thank you so much. As a matter of fact using elements->saveElement does the job exactly as if it was a "regular" entry element... I was searching too far ! :D Thanks again.  

---

