Answers.Body: <p>To assign a related Element (i.e. related User, Category, Entry, Tag, Asset) the value should be set to an array of element ids (even if there is only one element).</p>
  
  <pre><code>public function post(UserModel $user, EntryModel $ticket, $body)
  {
      $comment = new EntryModel();
      $comment-&gt;sectionId = craft()-&gt;sections-&gt;getSectionByHandle('comments')-&gt;id;
  
      $comment-&gt;setContent(array(
          'createdBy' =&gt; array($user-&gt;id),
          'ticket'    =&gt; array($ticket-&gt;id),
          'body'      =&gt; $body
      ));
  
      return craft()-&gt;entries-&gt;saveEntry($comment);
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-08T16:53:20.943Z
Answers.LastActivityDate: 2015-05-08T16:53:20.943Z
Answers.UserDisplayName: Douglas McDonald
Body: <p>I am trying to update the value of an <code>Entries</code> field on an <code>EntryModel</code>, however I am having a lot of trouble getting it to update correctly.</p>
  
  <p>I've tried allocating both an entire <code>EntryModel</code> and its <code>id</code> but neither seems to set the value, e.g.</p>
  
  <pre><code>$comment = new EntryModel();
  $comment-&gt;sectionId = $commentSectionId;
  
  $comment-&gt;setContent(array(
      'createdBy' =&gt; $user // or
      'createdBy' =&gt; $user-&gt;id
  ));
  
  craft()-&gt;entries-&gt;saveEntry($comment);
  </code></pre>
  
  <p>Where <code>$user</code> is a valid <code>EntryModel</code>.</p>
  
  <p>The comment saves fine, the <code>createdBy</code> field just has no value. I guess I am using the wrong data from <code>$user</code> but I have having a very hard time finding any thorough documentation around setting different kinds of EntryModel data.</p>
  
  <p>This is the entire bit of code I have, if it helps:</p>
  
  <pre><code>class Bugr_CommentService extends BaseApplicationComponent
  {
  
      public function post(EntryModel $user, EntryModel $ticket, $body)
      {
          $comment = new EntryModel();
          $comment-&gt;sectionId = craft()-&gt;sections-&gt;getSectionByHandle('comments')-&gt;id;
  
          $comment-&gt;setContent(array(
              'createdBy' =&gt; $user,
              'ticket' =&gt; $ticket,
              'body' =&gt; $body
          ));
  
          return craft()-&gt;entries-&gt;saveEntry($comment);
      }
  
  }
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-05-08T00:52:18.667Z
Comments.Text: Need to set the user id within an array. `'createdBy'=>array($user->id)` or `'createdBy'=>[$user->id]`. Very similar question [can be found here](http://craftcms.stackexchange.com/a/9569/784).
Comments.UserDisplayName: Douglas McDonald
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-05-08T00:53:41.100Z
Comments.Text: @DouglasMcDonald Ohh of course, because the entries field is obviously a list of entries. Sigh, makes perfect sense - thank you.
Comments.UserDisplayName: Marty
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-05-08T00:58:37.637Z
Comments.Text: Should also be using `getContent()->setAttributes([...])` (or `setContentFromPost()` if coming from post data) instead of `setContent()`. Check out the example I linked.
Comments.UserDisplayName: Douglas McDonald
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-05-08T01:02:11.370Z
Comments.Text: @DouglasMcDonald Sure, I'll keep that in mind. This is pretty much a duplicate but it would be nice if there was a more generic thread that covers both this and the linked questions.
Comments.UserDisplayName: Marty
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-05-08T01:06:54.917Z
Comments.Text: Btw... I assume that 'createdBy' is a custom field? To set the author you would obviously want to use `$entry->authorId = 1;` where 1 is the id of the user/author.
Comments.UserDisplayName: Douglas McDonald
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-05-08T01:13:51.227Z
Comments.Text: @DouglasMcDonald Of course :)
Comments.UserDisplayName: Marty
Tags: <plugin-development><entries><entries-field>
Title: Set EntryModel data on an element
ThreadText: # Set EntryModel data on an element
  
  (Asked by: username_0 on 2015-05-08)
  
  I am trying to update the value of an `Entries` field on an `EntryModel`, however I am having a lot of trouble getting it to update correctly.
  
  I've tried allocating both an entire `EntryModel` and its `id` but neither seems to set the value, e.g.
  
  ```
  $comment = new EntryModel();
  $comment->sectionId = $commentSectionId;
  
  $comment->setContent(array(
      'createdBy' => $user // or
      'createdBy' => $user->id
  ));
  
  craft()->entries->saveEntry($comment);
  
  ```
  
  Where `$user` is a valid `EntryModel`.
  
  The comment saves fine, the `createdBy` field just has no value. I guess I am using the wrong data from `$user` but I have having a very hard time finding any thorough documentation around setting different kinds of EntryModel data.
  
  This is the entire bit of code I have, if it helps:
  
  ```
  class Bugr_CommentService extends BaseApplicationComponent
  {
  
      public function post(EntryModel $user, EntryModel $ticket, $body)
      {
          $comment = new EntryModel();
          $comment->sectionId = craft()->sections->getSectionByHandle('comments')->id;
  
          $comment->setContent(array(
              'createdBy' => $user,
              'ticket' => $ticket,
              'body' => $body
          ));
  
          return craft()->entries->saveEntry($comment);
      }
  
  }
  
  ```
  
  **Tags:** plugin-development, entries, entries-field
  
  > **Comments:**
  > **username_1**: Need to set the user id within an array. `'createdBy'=>array($user->id)` or `'createdBy'=>[$user->id]`. Very similar question [can be found here](http://craftcms.stackexchange.com/a/9569/784).  
  > **username_0**: @username_1 Ohh of course, because the entries field is obviously a list of entries. Sigh, makes perfect sense - thank you.  
  > **username_1**: Should also be using `getContent()->setAttributes([...])` (or `setContentFromPost()` if coming from post data) instead of `setContent()`. Check out the example I linked.  
  > **username_0**: @username_1 Sure, I'll keep that in mind. This is pretty much a duplicate but it would be nice if there was a more generic thread that covers both this and the linked questions.  
  > **username_1**: Btw... I assume that 'createdBy' is a custom field? To set the author you would obviously want to use `$entry->authorId = 1;` where 1 is the id of the user/author.  
  > **username_0**: @username_1 Of course :)  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-05-08. Score: 1 | Accepted answer
  
  To assign a related Element (i.e. related User, Category, Entry, Tag, Asset) the value should be set to an array of element ids (even if there is only one element).
  
  ```
  public function post(UserModel $user, EntryModel $ticket, $body)
  {
      $comment = new EntryModel();
      $comment->sectionId = craft()->sections->getSectionByHandle('comments')->id;
  
      $comment->setContent(array(
          'createdBy' => array($user->id),
          'ticket'    => array($ticket->id),
          'body'      => $body
      ));
  
      return craft()->entries->saveEntry($comment);
  }
  
  ```
  
  
  ---
  
  