1

The following doesn't assign an existing user to a group. I've confirmed that both the user and the group exist. Any ideas?

$user = User::find()->username($username)->one();
$user->setGroups([Craft::$app->userGroups->getGroupById(1)]);
Craft::$app->elements->saveElement($user);

I found this answer, which will probably solve it for me, but it leaves me wondering what the purpose of setGroups is.

David Jones
  • 331
  • 2
  • 8

1 Answers1

1

Apparently this is the right way to do it:

$user = User::find()->username($username)->one();
Craft::$app->users->assignUserToGroups($user->id, [1]);

It's unclear how setGroups is supposed to work.

David Jones
  • 331
  • 2
  • 8