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'] ]);
stursby
  • 121
  • 1

1 Answers1

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);
}
Brad Bell
  • 67,440
  • 6
  • 73
  • 143