2

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'] ]);
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);
}
2
  • Hmmm @brad-bell, that doesn't seem to work... I'm still seeing a blank Company field in the CP. See here: bit.ly/1HO5YJO – stursby Jul 24 '15 at 15:07
  • Probably a validation error, then? What does $user->validate() return? – Brad Bell Jul 24 '15 at 16:59

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.