<p>Close! You should be using <a href="http://buildwithcraft.com/classreference/models/BaseElementModel#setContentFromPost-detail" rel="nofollow noreferrer">setContentFromPost()</a> for this.  See <a href="https://craftcms.stackexchange.com/a/6788/57">here</a> and <a href="https://craftcms.stackexchange.com/questions/4486/what-is-getcontent-the-contentmodel-and-how-do-i-use-it-to-get-and-set-cust">here</a> for more info.</p>

<p>You code would look like:</p>

<pre><code>$user = new UserModel();
$user-&gt;username  = 'tommy';
$user-&gt;firstName = 'Tom';
$user-&gt;lastName  = 'Foolery';
$user-&gt;email     = 'tom@thefoolery.com';
$user-&gt;setContentFromPost(array('company' =&gt; 'ACME'));

$success = craft()-&gt;users-&gt;saveUser($user);

if (!$success)
{
    Craft::log('Couldn’t save the user "'.$user-&gt;username.'"', LogLevel::Error);
}
</code></pre>

CC BY-SA 3.0
2015-07-24T15:07:10.177Z
Hmmm @brad-bell, that doesn't seem to work... I'm still seeing a blank Company field in the CP. See here: http://bit.ly/1HO5YJO
stursby
CC BY-SA 3.0
2015-07-24T16:59:45.380Z
Probably a validation error, then?  What does `$user->validate()` return?
Brad Bell
CC BY-SA 3.0
2015-07-23T22:54:38.670Z
2015-07-23T22:54:38.670Z
2017-04-13T12:51:00.253Z
Brad Bell
<p>I'm currently able to add a user with the following code (from <a href="http://buildwithcraft.com/classreference/services/UsersService#saveUser-detail" rel="nofollow">the Docs</a>):</p>

<pre><code>$user = new UserModel();
$user-&gt;username  = 'tommy';
$user-&gt;firstName = 'Tom';
$user-&gt;lastName  = 'Foolery';
$user-&gt;email     = 'tom@thefoolery.com';

$success = craft()-&gt;users-&gt;saveUser($user);

if (!$success)
{
    Craft::log('Couldn’t save the user "'.$user-&gt;username.'"', LogLevel::Error);
}
</code></pre>

<p>How can I also set the Profile Company name??</p>

<p>I've tried the following with no luck:</p>

<pre><code>$user-&gt;company = 'ACME';

$user-&gt;setContent([ 'fields' =&gt; ['company' =&gt; 'ACME'] ]);
</code></pre>

<users>
How do I programmatically set a Users company field?
# How do I programmatically set a Users company field?

(Asked by: username_0 on 2015-07-23)

I'm currently able to add a user with the following code (from the Docs):
$user = new UserModel();
$user->username  = 'tommy';
$user->firstName = 'Tom';
$user->lastName  = 'Foolery';
$user->email     = 'tom@thefoolery.com';

$success = craft()->users->saveUser($user);

if (!$success)
{
    Craft::log('Couldn’t save the user "'.$user->username.'"', LogLevel::Error);
}

How can I also set the Profile Company name??
I've tried the following with no luck:
$user->company = 'ACME';

$user->setContent([ 'fields' => ['company' => 'ACME'] ]);

**Tags:** users

---

## 1 answers

### Answer by username_1 on 2015-07-23. Score: 1

Close! You should be using setContentFromPost() for this.  See here and here for more info.
You code would look like:
$user = new UserModel();
$user->username  = 'tommy';
$user->firstName = 'Tom';
$user->lastName  = 'Foolery';
$user->email     = 'tom@thefoolery.com';
$user->setContentFromPost(array('company' => 'ACME'));

$success = craft()->users->saveUser($user);

if (!$success)
{
    Craft::log('Couldn’t save the user "'.$user->username.'"', LogLevel::Error);
}

> **Comments:**
> **username_0**: Hmmm @brad-bell, that doesn't seem to work... I'm still seeing a blank Company field in the CP. See here: http://bit.ly/1HO5YJO  
> **username_1**: Probably a validation error, then?  What does `$user->validate()` return?  

---

