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'
];
$entry->getContent()->setAttributes($attributes);
craft()->entries()->saveEntry($entry);
Craft
namespace. If your plugin isn't under theCraft
namespace (back in Craft 2 most plugins were as the examples were all like that) you'll need touse
it at the top of the plugin/class where your code is. souse Craft\EntryModel;
should do it. Or namespace the EntryModel right there$entry = new Craft\EntryModel();
new Entry();
)