I try to finish a plugin which imports data into my craftcms project. I already created a console based method/service, which I trigger/run in my shell. Inside my method(s) I receive data (XML or JSON) I parse my data and try to create and fill an entry of a specific channel I already created.

I tried "saveElement()" which doesn't work.

I found some tuts and informations for craftcms v2 (for example: https://docs.craftcms.com/api/v2/craft-entriesservice.html#public-methods)

Now i am stuck and i can not find any informations on how to solve this with craftcms v3.

Here is my last version of code after hours of different trys :(

$section = Craft::$app->sections->getSectionByHandle('testentry');
$entryTypes = $section->getEntryTypes();
$entryType = $entryTypes[0];
// Save Entry
//$entry = new EntryModel();
$entry = new \craft\elements\Entry();
$entry->sectionId = $section->id;
$entry->typeId = $entryType->id;
//$entry->locale = Craft::$app->i18n->getPrimarySiteLocaleId();
//$entry->authorId = 1;  // TODO:  Set author
$entry->enabled = true;
$entry->postDate = $post['post_date'];
$entry->slug = $post['post_name'];
//      $entry->getContent()->title = $post['post_title'];
//      $entry->setContentFromPost(array(
//          'body' => $postContent,
//          'categoryCareer' => NULL,
//      ));

if (Craft::$app->elements->saveElement($entry)) {
    $result = true;
else {
    echo 'Could not save the Job entry.'."\n";
    $result = false;
  • 1
    This looks mostly correct but there is likely a validation issue with your entry, required fields for example. Does outputting $entry->getErrors() reveal anything?
    – Ben Croker
    Nov 12, 2018 at 20:35
  • Oh no, i am so damn stupid. You are right, the last structure should work, but i did not display any errors anymore..... the title is required. So after i saw the error i was able to import several entries. Thanks a lot !!
    – Thomas
    Nov 13, 2018 at 17:36
  • Now i am able to continue, have to find out how to import and store images with an entry and that's it
    – Thomas
    Nov 13, 2018 at 17:44
  • Glad to hear it, I've added this as an answer so that you can mark it as solved.
    – Ben Croker
    Nov 13, 2018 at 22:16

1 Answer 1


As per your comments, the title field is required and causing a validation error. This can be resolved simply by setting the title.

$entry->title = 'The Title';

You can output the validation errors to be notified of any issues with saving the entries as follows.

if ($result === false) {

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.