Answers.Body: <p>When a User field is saved, it takes an array of user IDs only – not complete UserModel objects. Something like this should work:</p>
  
  <pre><code>// get IDs for current members of group 
  $members = $entry-&gt;members-&gt;ids();
  
  // add new user ID to members
  $members[] = $addUser-&gt;id;
  
  // update field with new $members array
  $entry-&gt;getContent()-&gt;setAttribute('members', $members);
  </code></pre>
  
  <p>The rest of your code looks fine, except for a minor typo – <code>$enries</code> should be <code>$entries</code>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-31T08:36:44.827Z
Answers.Text: THANK YOU! I can't express my gratitude enough. Just tried it and it worked. :)
  
  Out of curiosity and for future reference in troubleshooting, how were you able to see that the user field needed just an array of user IDs?
Answers.UserDisplayName: taylor
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-31T09:45:26.547Z
Answers.Text: Awesome, happy to help out :) All of the relational fieldtypes (i.e. Entries, Users, Categories, Assets and Tags) take an array of IDs – I honestly can't remember how/where I learned that, though a very easy way to find out how the input data should look for a certain fieldtype is to look at the request headers (e.g. in your browser's dev tools) when the entry is saved in the CP. Other than that, you can look at the relevant classes in the [class reference](https://craftcms.com/classreference), i.e. `UsersFieldType`, `EntriesService` etc., to get a feel for how things are put together.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-31T08:19:47.170Z
Answers.LastActivityDate: 2015-12-31T08:19:47.170Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>I have a <code>channel</code> called "Groups", with a <code>users</code> field type called <code>members</code> for the entries. I need to be able to add/remove users to the <code>members</code> entry field via plugin.</p>
  
  <p>What I've tried is:</p>
  
  <pre><code>// criteria
  $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
  
  // filter
  $criteria-&gt;slug     = 'groupNameHere';
  
  // section
  $criteria-&gt;section('groups');
  
  // get entry
  $entries = $criteria-&gt;find();
  
  foreach($enries as $entry)
  {
      // get current members of group 
      $members = $entry['members'];
  
      // add new user model to $members
      $members[] = craft()-&gt;users-&gt;getUserByUsernameOrEmail("newUserEmailHere");
  
      // update field with new $members array
      $entry-&gt;getContent()-&gt;setAttribute('members', $members);
  
      // save
      craft()-&gt;entries-&gt;saveEntry($entry);
  }
  </code></pre>
  
  <p>This simply clears the previous <code>members</code> value for the group, without even adding the new user. I can't find anything online about updating this type of field-type via plugin so I'm just guessing how to do this. Does anyone know the right way to do this?</p>
  
  <p>Any information would hugely help! Thanks!</p>
  
Tags: <plugin-development><users><fieldtypes><attribute>
Title: Update User "Field Type" for Entry in Plugin
ThreadText: # Update User "Field Type" for Entry in Plugin
  
  (Asked by: username_0 on 2015-12-30)
  
  I have a `channel` called "Groups", with a `users` field type called `members` for the entries. I need to be able to add/remove users to the `members` entry field via plugin.
  
  What I've tried is:
  
  ```
  // criteria
  $criteria = craft()->elements->getCriteria(ElementType::Entry);
  
  // filter
  $criteria->slug     = 'groupNameHere';
  
  // section
  $criteria->section('groups');
  
  // get entry
  $entries = $criteria->find();
  
  foreach($enries as $entry)
  {
      // get current members of group 
      $members = $entry['members'];
  
      // add new user model to $members
      $members[] = craft()->users->getUserByUsernameOrEmail("newUserEmailHere");
  
      // update field with new $members array
      $entry->getContent()->setAttribute('members', $members);
  
      // save
      craft()->entries->saveEntry($entry);
  }
  
  ```
  
  This simply clears the previous `members` value for the group, without even adding the new user. I can't find anything online about updating this type of field-type via plugin so I'm just guessing how to do this. Does anyone know the right way to do this?
  
  Any information would hugely help! Thanks!
  
  **Tags:** plugin-development, users, fieldtypes, attribute
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-12-31. Score: 4 | Accepted answer
  
  When a User field is saved, it takes an array of user IDs only – not complete UserModel objects. Something like this should work:
  
  ```
  // get IDs for current members of group 
  $members = $entry->members->ids();
  
  // add new user ID to members
  $members[] = $addUser->id;
  
  // update field with new $members array
  $entry->getContent()->setAttribute('members', $members);
  
  ```
  
  The rest of your code looks fine, except for a minor typo – `$enries` should be `$entries`.
  
  > **Comments:**
  > **username_0**: THANK YOU! I can't express my gratitude enough. Just tried it and it worked. :)  Out of curiosity and for future reference in troubleshooting, how were you able to see that the user field needed just an array of user IDs?  
  > **username_1**: Awesome, happy to help out :) All of the relational fieldtypes (i.e. Entries, Users, Categories, Assets and Tags) take an array of IDs – I honestly can't remember how/where I learned that, though a very easy way to find out how the input data should look for a certain fieldtype is to look at the request headers (e.g. in your browser's dev tools) when the entry is saved in the CP. Other than that, you can look at the relevant classes in the [class reference](https://craftcms.com/classreference), i.e. `UsersFieldType`, `EntriesService` etc., to get a feel for how things are put together.  
  
  ---
  
  