I want to create a new entry for a section named 'drinks'(having section id= 6). The namespace of the file having this code is namespace putyourlightson\userguide\controllers;.I copied this code from StackExchange. My first line is getting an error(Class 'Craft\EntryModel' not found).

$entry = new \Craft\EntryModel();

$attributes = [
  'section'   => $yourSectionId,
  'entryType' => $yourEntryTypeId,
  'field1'    => 'foo',
  'field2'    => 'bar'



How should you import it?enter image description here

  • 1
    Do you mean add a new section model, or a new entry type maybe? (Because line one in your code is creating a new entry model) If you're just wondering how to save an entry as it looks like above, check the docs here Apr 1, 2020 at 10:03
  • ok I was not clear ...1st line is not working for me.my question should have been how do you import EntryModel
    – jamesSeb
    Apr 1, 2020 at 10:17
  • 1
    The EntryModel class is under the Craft namespace. If your plugin isn't under the Craft namespace (back in Craft 2 most plugins were as the examples were all like that) you'll need to use it at the top of the plugin/class where your code is. so use Craft\EntryModel; should do it. Or namespace the EntryModel right there $entry = new Craft\EntryModel(); Apr 1, 2020 at 10:31
  • but still it is showing ERROR -Class 'Craft\EntryModel' not found
    – jamesSeb
    Apr 1, 2020 at 10:35
  • 1
    Ahh, you're trying to use Craft 2 code in Craft 3. You've just looked at the wrong docs. Try this link for the Craft 3 version. (Most things have dropped "Model" from their names so you want new Entry();) Apr 1, 2020 at 10:51

3 Answers 3


Just so there's an answer here. OP was using Craft 2 code in a Craft 3 environment.

The code should be something like this in Craft 3:

$entry = new Entry();
$entry->sectionId = $yourSectionId;
$entry->typeId = $yourEntryTypeId;
$entry->authorId = $yourAuthorId;
$entry->enabled = true;
$entry->title = "Foo";
    'field1' => "Foo",
    'field2' => "Bar",
$success = Craft::$app->elements->saveElement($entry);
if (!$success) {
    Craft::error('Couldn’t save the entry "'.$entry->title.'"', __METHOD__);
  • still not solving my problem.First line is showing error.How can you import the Entry class correctly?
    – jamesSeb
    Apr 1, 2020 at 11:02
  • I have editeed my qn for better clarity
    – jamesSeb
    Apr 1, 2020 at 11:04
  • Hi, sorry I just saw this again. I think my previous comment on how to import (use) PHP classes in your code covers that. If you'd like to read more on that, the PHP docs are worth a look. You could do something like use craft\elements\Entry as MySpecificUseOfAnEntry; and then when you want to new up that class do $entry = new MySpecificUseOfAnEntry(); and the code above, with those changes, will work fine. Apr 2, 2020 at 20:27

Either use

use craft\elements\Entry;
$entry = new Entry();


$entry = new \craft\elements\Entry();

You can download and see guest-entries plugin code to correctly see how to save an entry from a plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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