<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>

CC BY-SA 3.0
2015-05-08T16:53:20.943Z
2015-05-08T16:53:20.943Z
Douglas McDonald
<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>

CC BY-SA 3.0
2015-05-08T00:52:18.667Z
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).
Douglas McDonald
CC BY-SA 3.0
2015-05-08T00:53:41.100Z
@DouglasMcDonald Ohh of course, because the entries field is obviously a list of entries. Sigh, makes perfect sense - thank you.
Marty
CC BY-SA 3.0
2015-05-08T00:58:37.637Z
Should also be using `getContent()->setAttributes([...])` (or `setContentFromPost()` if coming from post data) instead of `setContent()`. Check out the example I linked.
Douglas McDonald
CC BY-SA 3.0
2015-05-08T01:02:11.370Z
@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.
Marty
CC BY-SA 3.0
2015-05-08T01:06:54.917Z
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.
Douglas McDonald
CC BY-SA 3.0
2015-05-08T01:13:51.227Z
@DouglasMcDonald Of course :)
Marty
<plugin-development><entries><entries-field>
Set EntryModel data on an element
# 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**: @DouglasMcDonald 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**: @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.  
> **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**: @DouglasMcDonald 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);
}


---

