<p>So it seems the only way to achieve this is to assign the user to the group, not the other way round (assign the group to the user).</p>

<p>So once my user has saved I can call following:</p>

<p><code>craft()-&gt;userGroups-&gt;assignUserToGroups($user-&gt;id, $groupId);</code></p>

CC BY-SA 3.0
2017-09-27T10:22:53.483Z
2017-09-27T10:22:53.483Z
Cole Henley
<p>Anyone had experience of assigning a user group when saving or registering a user via a plugin?</p>

<p>Tried various combinations of <code>$user-&gt;group = 1</code>, <code>$user-&gt;group = [1]</code>, <code>$user-&gt;setGroups([$group])</code> and <code>$user-&gt;setGroups($group)</code> (where $group is the group I want to assign the user to) but whilst the user is being saved fine the user group is not assigned.</p>

<p>Any thoughts appreciated!</p>

<p>Cheers,</p>

<p>Cole</p>

<plugin-development><users><user-groups><usermodel>
Saving/assigning User Group via a plugin
# Saving/assigning User Group via a plugin

(Asked by: username_0 on 2017-09-27)

Anyone had experience of assigning a user group when saving or registering a user via a plugin?

Tried various combinations of `$user->group = 1`, `$user->group = [1]`, `$user->setGroups([$group])` and `$user->setGroups($group)` (where $group is the group I want to assign the user to) but whilst the user is being saved fine the user group is not assigned.

Any thoughts appreciated!

Cheers,

Cole

**Tags:** plugin-development, users, user-groups, usermodel

---

## 1 answers

### Answer by username_0 on 2017-09-27. Score: 3

So it seems the only way to achieve this is to assign the user to the group, not the other way round (assign the group to the user).

So once my user has saved I can call following:

`craft()->userGroups->assignUserToGroups($user->id, $groupId);`


---

