Answers.Body: <p>By checking <code>vendor/craftcms/cms/src/controllers/UsersController.php</code> and <code>vendor/craftcms/cms/src/base/Element.php</code>, I did it like </p>
  
  <pre><code>$user-&gt;setFieldValue('products', $userProductsIds);
  return Craft::$app-&gt;getElements()-&gt;saveElement($user, false);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-28T11:59:01.360Z
Answers.LastActivityDate: 2019-06-28T11:59:01.360Z
Answers.UserDisplayName: Jorge Anzola
Body: <p>From <a href="https://craftcms.stackexchange.com/questions/16593/how-can-i-programmatically-set-the-value-of-an-entries-field-type">this</a> question, I tried:</p>
  
  <pre><code>$user-&gt;setAttributes(['products' =&gt; $userProductsIds]);
  
  return Craft::$app-&gt;getElements()-&gt;saveElement($user);
  </code></pre>
  
  <p>Where <code>$user</code> is an instance of <code>craft\elements\User;</code> and <code>$userProductsIds</code> an array. It's not giving me any errors, but the user is not updated. Any idea on what am I missing?</p>
  
Tags: <craft3><entries><users><usermodel>
Title: Set User's entry field from PHP in Craft 3
ThreadText: # Set User's entry field from PHP in Craft 3
  
  (Asked by: username_0 on 2019-06-28)
  
  From [this](https://craftcms.stackexchange.com/questions/16593/how-can-i-programmatically-set-the-value-of-an-entries-field-type) question, I tried:
  
  ```
  $user->setAttributes(['products' => $userProductsIds]);
  
  return Craft::$app->getElements()->saveElement($user);
  
  ```
  
  Where `$user` is an instance of `craft\elements\User;` and `$userProductsIds` an array. It's not giving me any errors, but the user is not updated. Any idea on what am I missing?
  
  **Tags:** craft3, entries, users, usermodel
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2019-06-28. Score: 1 | Accepted answer
  
  By checking `vendor/craftcms/cms/src/controllers/UsersController.php` and `vendor/craftcms/cms/src/base/Element.php`, I did it like
  
  ```
  $user->setFieldValue('products', $userProductsIds);
  return Craft::$app->getElements()->saveElement($user, false);
  
  ```
  
  
  ---
  
  