Answers.Body: <p><a href="http://buildwithcraft.com/classreference/services/UserGroupsService#assignUserToGroups-detail" rel="nofollow">UserGroupsService->assignUserToGroups</a> takes a user's ID as the first parameter and you're passing in the entire <a href="http://buildwithcraft.com/docs/templating/usermodel" rel="nofollow">UserModel</a>.</p>
  
  <p>Change it to this and it should work:</p>
  
  <pre><code>craft()-&gt;userGroups-&gt;assignUserToGroups($user-&gt;id, $groupIds); 
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-17T01:19:14.000Z
Answers.LastActivityDate: 2015-11-17T01:19:14.000Z
Answers.UserDisplayName: Brad Bell
Body: <pre><code>$groupIds = [1, 2];
  craft()-&gt;userGroups-&gt;assignUserToGroups($user, $groupIds);
  </code></pre>
  
  <p>When executed I get the following error:</p>
  
  <pre><code>CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity     constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`letshacklondon2016`.`craft_usergroups_users`, CONSTRAINT `craft_usergroups_users_userId_fk` FOREIGN KEY (`userId`) REFERENCES `craft_users` (`id`) ON DELETE CASCADE). The SQL statement executed was: INSERT INTO `craft_usergroups_users` (`groupId`, `userId`, `dateCreated`, `dateUpdated`, `uid`) VALUES (:row0_col0, :row0_col1, :row0_col2, :row0_col3, :row0_col4), (:row1_col0, :row1_col1, :row1_col2, :row1_col3, :row1_col4). Bound with :row0_col0=1, :row0_col1=Craft\UserModel::__set_state(array('elementType' =&gt; 'User', ... (etc.)
  </code></pre>
  
  <p>Any Ideas?</p>
  
Tags: <plugin-development><user-groups>
Title: Setting User's Groups - Integrity Constraint Error
ThreadText: # Setting User's Groups - Integrity Constraint Error
  
  (Asked by: username_0 on 2015-11-16)
  
  ```
  $groupIds = [1, 2];
  craft()->userGroups->assignUserToGroups($user, $groupIds);
  
  ```
  
  When executed I get the following error:
  
  ```
  CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity     constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`letshacklondon2016`.`craft_usergroups_users`, CONSTRAINT `craft_usergroups_users_userId_fk` FOREIGN KEY (`userId`) REFERENCES `craft_users` (`id`) ON DELETE CASCADE). The SQL statement executed was: INSERT INTO `craft_usergroups_users` (`groupId`, `userId`, `dateCreated`, `dateUpdated`, `uid`) VALUES (:row0_col0, :row0_col1, :row0_col2, :row0_col3, :row0_col4), (:row1_col0, :row1_col1, :row1_col2, :row1_col3, :row1_col4). Bound with :row0_col0=1, :row0_col1=Craft\UserModel::__set_state(array('elementType' => 'User', ... (etc.)
  
  ```
  
  Any Ideas?
  
  **Tags:** plugin-development, user-groups
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-11-17. Score: 1 | Accepted answer
  
  [UserGroupsService->assignUserToGroups](http://buildwithcraft.com/classreference/services/UserGroupsService#assignUserToGroups-detail) takes a user's ID as the first parameter and you're passing in the entire [UserModel](http://buildwithcraft.com/docs/templating/usermodel).
  
  Change it to this and it should work:
  
  ```
  craft()->userGroups->assignUserToGroups($user->id, $groupIds); 
  
  ```
  
  
  ---
  
  