<p>Not sure if there is more to your code or not but looks like you need to set the <code>title</code> as well as other required fields. In addition the entries field is expecting an array of <code>entryId</code>s.</p>

<p>Here is a link to the saveEntry method in the EntriesService, which shows the required fields:
<a href="https://craftcms.com/classreference/services/EntriesService#saveEntry-detail" rel="nofollow noreferrer">https://craftcms.com/classreference/services/EntriesService#saveEntry-detail</a></p>

<p>This answer is somewhat similar in that they are relating an entry, but you can see that the <code>entryField</code> is being set with an <code>array</code>.</p>

<p><a href="https://craftcms.stackexchange.com/questions/14560/how-do-i-relate-a-user-to-a-channel-entry-from-a-form/14563#14563">How do I relate a user to a channel entry from a form?</a></p>

CC BY-SA 3.0
2016-04-14T09:47:29.577Z
Yes, the issue was a required ```title```. And ```$newsItem->getErrors()``` showed this error to me. thanks @Brad
Rbijker.com
CC BY-SA 3.0
2016-04-07T17:23:21.440Z
2016-04-07T18:04:01.763Z
2017-04-13T12:51:00.303Z
mcclaskiem
<p>Along the same train of thought as <a href="https://craftcms.stackexchange.com/a/14567/57">mcclaskiem's</a> answer, you're likely getting a validation error.</p>

<p>If you check <code>$newsItem-&gt;getErrors()</code> after you call <code>craft()-&gt;entries-&gt;saveEntry($newsItem);</code>, you can see which attributes are failing validation and why.</p>

CC BY-SA 3.0
2016-04-07T18:35:13.383Z
2016-04-07T18:35:13.383Z
2017-04-13T12:51:00.200Z
Brad Bell
<p>I am trying to store an entry in a plugin. The entry has a field 'tags' that relates to another entry. I am trying to store it with the following code.  </p>

<pre><code>$newsItem = new EntryModel();
$newsItem-&gt;sectionId = craft()-&gt;sections-&gt;getSectionByHandle('news')-&gt;id;
$newsItem-&gt;getContent()-&gt;tags = $tag-&gt;id;
craft()-&gt;entries-&gt;saveEntry($newsItem);
</code></pre>

<p>When I <code>var_dump(craft()-&gt;entries-&gt;saveEntry($newsItem))</code> I get a <code>false</code> response. </p>

<p>Also, in this situation we cannot use the method <code>setContentFromPost()</code> so we need to do it from within the plugin and not from a front-end form.</p>

<plugin-development>
Issue with storing an Entry
# Issue with storing an Entry

(Asked by: username_0 on 2016-04-07)

I am trying to store an entry in a plugin. The entry has a field 'tags' that relates to another entry. I am trying to store it with the following code.  
$newsItem = new EntryModel();
$newsItem->sectionId = craft()->sections->getSectionByHandle('news')->id;
$newsItem->getContent()->tags = $tag->id;
craft()->entries->saveEntry($newsItem);

When I var_dump(craft()->entries->saveEntry($newsItem)) I get a false response. 
Also, in this situation we cannot use the method setContentFromPost() so we need to do it from within the plugin and not from a front-end form.

**Tags:** plugin-development


---

## 2 answers

### Answer by username_1 on 2016-04-07. Score: 3 | Accepted answer

Not sure if there is more to your code or not but looks like you need to set the `title` as well as other required fields. In addition the entries field is expecting an array of `entryId`s.

Here is a link to the saveEntry method in the EntriesService, which shows the required fields:
<https://craftcms.com/classreference/services/EntriesService#saveEntry-detail>

This answer is somewhat similar in that they are relating an entry, but you can see that the `entryField` is being set with an `array`.

[How do I relate a user to a channel entry from a form?](https://craftcms.stackexchange.com/questions/14560/how-do-i-relate-a-user-to-a-channel-entry-from-a-form/14563#14563)

> **Comments:**
> **username_0**: Yes, the issue was a required ```title```. And ```$newsItem->getErrors()``` showed this error to me. thanks @Brad  

---

### Answer by username_2 on 2016-04-07. Score: 1

Along the same train of thought as [username_1's](https://craftcms.stackexchange.com/a/14567/57) answer, you're likely getting a validation error.

If you check `$newsItem->getErrors()` after you call `craft()->entries->saveEntry($newsItem);`, you can see which attributes are failing validation and why.


---

