Answers.Body: <p>Relationships are always added as arrays of IDs, even if you are only relating a single element:</p>
  
  <pre><code>$user-&gt;setContentFromPost(array(
      'companyName' =&gt; $admin-&gt;companyName, 
      'companyId' =&gt; $admin-&gt;companyId, 
      'partnerManager' =&gt; array($admin-&gt;partnerManager[0]-&gt;id)
  ));
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-07T11:45:51.777Z
Answers.LastActivityDate: 2019-01-07T11:45:51.777Z
Answers.UserDisplayName: Mike Pepper
Body: <p>I'm creating a new user using <code>$user-&gt;setContentFromPost()</code>. It all works fine except I can't attach an entry to an Entries field.</p>
  
  <pre><code>$user = new UserModel();
  $user-&gt;username         = $email;
  $user-&gt;email            = $email;
  $user-&gt;unverifiedEmail  = $email;
  
  $user-&gt;setContentFromPost(array(
    'companyName' =&gt; $admin-&gt;companyName, 
    'companyId' =&gt; $admin-&gt;companyId, 
    'partnerManager[]' =&gt; $admin-&gt;partnerManager[0]-&gt;id
  ));
  </code></pre>
  
  <p>This will create user but the <code>partnerManager</code> Entries field will be empty. I've tried a few different syntax variations, but none of them seem to work:</p>
  
  <pre><code>'fields[partnerManager][]' =&gt; $admin-&gt;partnerManager[0]-&gt;id
  'partnerManager' =&gt; $admin-&gt;partnerManager[0]-&gt;id
  'partnerManager[0]' =&gt; $admin-&gt;partnerManager[0]-&gt;id
  </code></pre>
  
  <p>How to fill this field?</p>
  
Tags: <plugin-development><usermodel><craft2>
Title: Adding entry type using setContentFromPost
ThreadText: # Adding entry type using setContentFromPost
  
  (Asked by: username_0 on 2018-12-20)
  
  I'm creating a new user using `$user->setContentFromPost()`. It all works fine except I can't attach an entry to an Entries field.
  
  ```
  $user = new UserModel();
  $user->username         = $email;
  $user->email            = $email;
  $user->unverifiedEmail  = $email;
  
  $user->setContentFromPost(array(
    'companyName' => $admin->companyName, 
    'companyId' => $admin->companyId, 
    'partnerManager[]' => $admin->partnerManager[0]->id
  ));
  
  ```
  
  This will create user but the `partnerManager` Entries field will be empty. I've tried a few different syntax variations, but none of them seem to work:
  
  ```
  'fields[partnerManager][]' => $admin->partnerManager[0]->id
  'partnerManager' => $admin->partnerManager[0]->id
  'partnerManager[0]' => $admin->partnerManager[0]->id
  
  ```
  
  How to fill this field?
  
  **Tags:** plugin-development, usermodel, craft2
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-01-07. Score: 2 | Accepted answer
  
  Relationships are always added as arrays of IDs, even if you are only relating a single element:
  
  ```
  $user->setContentFromPost(array(
      'companyName' => $admin->companyName, 
      'companyId' => $admin->companyId, 
      'partnerManager' => array($admin->partnerManager[0]->id)
  ));
  
  ```
  
  
  ---
  
  