Answers.Body: <p>It's not documented, but <code>UserGroupsService</code> has a method <code>assignUserToGroups($userId, int|array $groupIds)</code>. You'll have to save your user first, but once you have a user id, I believe you can just use that with something like this:</p>
  
  <pre class="lang-php prettyprint-override"><code>// Original code
  $user = new UserModel;
  
  $user-&gt;username = 'username';
  $user-&gt;email = 'user@email.com';
  $user-&gt;firstName = 'first name';
  $user-&gt;lastName = 'last name';
  $user-&gt;getContent()-&gt;setAttributes([ 'usersAddress' =&gt; 'Your face' ]);
  
  craft()-&gt;users-&gt;saveUser($user);
  
  // Add to single group
  craft()-&gt;userGroups-&gt;assignUserToGroups($user-&gt;id, $groupId);
  
  // OR, add to multiple groups
  craft()-&gt;userGroups-&gt;assignUserToGroups($user-&gt;id, [$groupIdOne, $groupIdTwo]);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-07T15:25:47.047Z
Answers.Text: I'll try this. If anyone else know how to do it without saving first that's ideal. But as always Matt Stauffer you handsome intelligent bastard you.
Answers.UserDisplayName: Matt Green
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-07T15:26:26.747Z
Answers.Text: HA! How YOU doin...
Answers.UserDisplayName: Matt Stauffer
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-07T15:48:07.097Z
Answers.Text: All worked great! Hope they document that somewhere.
Answers.UserDisplayName: Matt Green
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-07T15:24:00.720Z
Answers.LastActivityDate: 2017-06-19T21:06:24.967Z
Answers.LastEditDate: 2017-06-19T21:06:24.967Z
Answers.UserDisplayName: Matt Stauffer
Body: <p>I am using the <code>UserModel</code> to create a user, and then injecting that into the User API service as is instructed in the documentation. The only thing I am not seeing is how to set the group. </p>
  
  <p>I looked in the Model class and there is no method for setting the group and it's not part of the <code>defineAttributes()</code> method. </p>
  
  <p>In the documentation there is no method to set group, just read only the groups: <a href="https://craftcms.com/docs/templating/usermodel#getGroups" rel="nofollow noreferrer">https://craftcms.com/docs/templating/usermodel#getGroups</a></p>
  
  <p>Nothing here about it: <a href="https://craftcms.com/classreference/services/UsersService" rel="nofollow noreferrer">https://craftcms.com/classreference/services/UsersService</a></p>
  
  <p>Here is what I am doing to save user.</p>
  
  <pre class="lang-php prettyprint-override"><code>$user = new UserModel;
  
  $user-&gt;username = 'username';
  $user-&gt;email = 'yourmom@email.com';
  // $user-&gt;groupID = "group";
  $user-&gt;firstName = 'first name';
  $user-&gt;lastName = 'last name';
  $user-&gt;getContent()-&gt;setAttributes([ 'usersAddress' =&gt; 'Your face' ]);
  
  craft()-&gt;users-&gt;saveUser($user);
  </code></pre>
  
  <p>Am I missing something obvious?</p>
  
Tags: <users><php>
Title: How do you set a users group?
ThreadText: # How do you set a users group?
  
  (Asked by: username_0 on 2014-08-07)
  
  I am using the `UserModel` to create a user, and then injecting that into the User API service as is instructed in the documentation. The only thing I am not seeing is how to set the group.
  
  I looked in the Model class and there is no method for setting the group and it's not part of the `defineAttributes()` method.
  
  In the documentation there is no method to set group, just read only the groups: <https://craftcms.com/docs/templating/usermodel#getGroups>
  
  Nothing here about it: <https://craftcms.com/classreference/services/UsersService>
  
  Here is what I am doing to save user.
  
  ```php
  $user = new UserModel;
  
  $user->username = 'username';
  $user->email = 'yourmom@email.com';
  // $user->groupID = "group";
  $user->firstName = 'first name';
  $user->lastName = 'last name';
  $user->getContent()->setAttributes([ 'usersAddress' => 'Your face' ]);
  
  craft()->users->saveUser($user);
  
  ```
  
  Am I missing something obvious?
  
  **Tags:** users, php
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-08-07. Score: 6 | Accepted answer
  
  It's not documented, but `UserGroupsService` has a method `assignUserToGroups($userId, int|array $groupIds)`. You'll have to save your user first, but once you have a user id, I believe you can just use that with something like this:
  
  ```php
  // Original code
  $user = new UserModel;
  
  $user->username = 'username';
  $user->email = 'user@email.com';
  $user->firstName = 'first name';
  $user->lastName = 'last name';
  $user->getContent()->setAttributes([ 'usersAddress' => 'Your face' ]);
  
  craft()->users->saveUser($user);
  
  // Add to single group
  craft()->userGroups->assignUserToGroups($user->id, $groupId);
  
  // OR, add to multiple groups
  craft()->userGroups->assignUserToGroups($user->id, [$groupIdOne, $groupIdTwo]);
  
  ```
  
  > **Comments:**
  > **username_0**: I'll try this. If anyone else know how to do it without saving first that's ideal. But as always username_0 username_1 you handsome intelligent bastard you.  
  > **username_1**: HA! How YOU doin...  
  > **username_0**: All worked great! Hope they document that somewhere.  
  
  ---
  
  