Answers.Body: <p>Relational fields are always stored as arrays, even if you are limiting them to only one entry so this should work:</p>
  
  <pre><code>$entry-&gt;setContentFromPost(['userAccount' =&gt; array($user-&gt;id)]);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-04T12:14:01.493Z
Answers.Text: That was it. I knew I was close - just wasn't sure how to send the right "stuff" to set the field. Thank you!
Answers.UserDisplayName: Damon
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-04T10:45:22.973Z
Answers.LastActivityDate: 2015-12-04T10:45:22.973Z
Answers.UserDisplayName: Steve Rowling
Body: <p>I have a plugin that listens for the <code>users.onSaveUser</code>. So when users register, an account is created, and I am creating an entry that will contain extended profile information. I am attempting to create a user field on that newly created entry to relate the entry back to the user account.</p>
  
  <p><strong>MyPlugin.php</strong></p>
  
  <pre><code>$entry = new EntryModel();
  $entry-&gt;sectionId = 16;
  $entry-&gt;typeId = 1;
  $entry-&gt;enabled = false;
  $entry-&gt;authorId = $user-&gt;id;
  
  $entry-&gt;getContent()-&gt;title = craft()-&gt;request-&gt;getPost('firstName') . ' ' . craft()-&gt;request-&gt;getPost('lastName');
  
  // userAccount is the handle of the user field.
  $entry-&gt;setContent(['fields' =&gt; ['userAccount' =&gt; $user-&gt;id]]);  // nope.
  $entry-&gt;setContentFromPost(['userAccount' =&gt; $user-&gt;id]);        // nope.
  
  ...
  
  craft()-&gt;entries-&gt;saveEntry($entry);
  </code></pre>
  
  <p>Everything is coming across except the user field. I'm not sure what the field needs/is looking for to set it's value.</p>
  
Tags: <plugin-development><users>
Title: How can I populate a user field via a plugin?
ThreadText: # How can I populate a user field via a plugin?
  
  (Asked by: username_0 on 2015-12-04)
  
  I have a plugin that listens for the `users.onSaveUser`. So when users register, an account is created, and I am creating an entry that will contain extended profile information. I am attempting to create a user field on that newly created entry to relate the entry back to the user account.
  
  **MyPlugin.php**
  
  ```
  $entry = new EntryModel();
  $entry->sectionId = 16;
  $entry->typeId = 1;
  $entry->enabled = false;
  $entry->authorId = $user->id;
  
  $entry->getContent()->title = craft()->request->getPost('firstName') . ' ' . craft()->request->getPost('lastName');
  
  // userAccount is the handle of the user field.
  $entry->setContent(['fields' => ['userAccount' => $user->id]]);  // nope.
  $entry->setContentFromPost(['userAccount' => $user->id]);        // nope.
  
  ...
  
  craft()->entries->saveEntry($entry);
  
  ```
  
  Everything is coming across except the user field. I'm not sure what the field needs/is looking for to set it's value.
  
  **Tags:** plugin-development, users
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-12-04. Score: 1 | Accepted answer
  
  Relational fields are always stored as arrays, even if you are limiting them to only one entry so this should work:
  
  ```
  $entry->setContentFromPost(['userAccount' => array($user->id)]);
  
  ```
  
  > **Comments:**
  > **username_0**: That was it. I knew I was close - just wasn't sure how to send the right "stuff" to set the field. Thank you!  
  
  ---
  
  