I would like to migrate content from external source to my Section model. So I have to create Entries manually. Is there any better way to do this beside this snippet from the documentation:
$entry = new Entry();
$entry->sectionId = 10;
$entry->typeId = 1;
$entry->fieldLayoutId = $entry->getType()->fieldLayoutId;
$entry->authorId = 5;
$entry->enabled = true;
$entry->title = "Hello World!";
$entry->setFieldValues([
'body' => "<p>I can’t believe I literally just called this “Hello World!”.</p>",
]);
$success = Craft::$app->elements->saveElement($entry);
if (!$success) {
Craft::error('Couldn’t save the entry "'.$entry->title.'"', __METHOD__);
}
reference: https://docs.craftcms.com/api/v3/craft-services-elements.html#saveElement()-detail
I am looking for a factory method, or a Builder to properly create the object. Using EntityController is not an option, because I would like to use it from CommandLine interface.
EntriesControlleris not only for CP requests. You can use that for frontend requests too. I can create a form for you if you want but I have trouble to fully understand what you want – Robin Schambach Mar 05 '18 at 06:17EntriesControllerand change it to extend the console controller http://www.yiiframework.com/doc-2.0/guide-tutorial-console.html would that be an option for you? – Robin Schambach Mar 05 '18 at 06:49