Set UserGroups for existing User Using Craft::Pro Edition, I want to add a specific UserGroup to an existing User in my module. I tried it this way, but the group is not saved.

$userGroup = Craft::$app-&gt;userGroups-&gt;getGroupByHandle('partner');
$user-&gt;setGroups([$userGroup]);
$success = Craft::$app-&gt;elements-&gt;saveElement($user);


After (successful) saveElement(), $user->getGroups() still returns an empty array.

When I add the group in the Admin CP, it works, but it needs a password confirmation.
You can assign a user to certain groups via

Craft::$app-&gt;getUsers()-&gt;assignUserToGroups($user-&gt;id, [1,2,3]);


Would assign them to the groups with the ids 1, 2 and 3