0

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.

3

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

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?

1
  • Yes, the issue was a required title. And $newsItem->getErrors() showed this error to me. thanks @Brad
    – SiteHopper
    Apr 14 '16 at 9:47
1

Along the same train of thought as mcclaskiem's 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.