1

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.

  • First of all, your question is very confusing. Please create a new question for every single question you have. Do you want to know how to save a section or do you want to know how to save an entry? The EntriesController is 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 5 '18 at 6:17
  • I would like to use it with command line tool so the controller is not an option. And yes the goal is to create an Entry from command line – Visky Máté Mar 5 '18 at 6:26
  • You could copy the EntriesController and change it to extend the console controller yiiframework.com/doc-2.0/guide-tutorial-console.html would that be an option for you? – Robin Schambach Mar 5 '18 at 6:49
  • Hm that us not the best option. The trouble is not the create but the supportability. If i copy something from the framework that is never a great idea i guess. But i cann accept if there is no builder/factory method to achive this one. Alternatively i guess it is better if i call the controller from http guzzle i guess. Than i do not have to copy anything:) – Visky Máté Mar 5 '18 at 6:53
  • Copying from the framework is mostly the best you can do since you'll we sure the code works properly if you don't have any idea what to do. At least it's far better than downloading plugins. I don't think there is a build in function to achieve this but it's really trivial from what I've seen but if you don't want to create a custom controller I'm not sure what to do either – Robin Schambach Mar 5 '18 at 6:59
1

This is the best answer so far. Basically the easiest way is to use array, and map everything there. The saveElement function will the the rest. You can update fields, tags, categories, matrixTables etc. The given example is matrix-data only, but again the same logic could be used everywhere.

How to save Matrix data programmatically in Craft 3

The only missing point is the "merge" functionality but I guess it has to be done manually after using the toArray() function first

|improve this answer|||||
  • What does this has to do with your question? I though you wanted to know how to do it via command line? Your code it actually exactly the same like he does. you just used another syntax form instead of the Yii::configure function (and it's nearly the same like Craft does everything, that's why I suggested to look into their controller) – Robin Schambach Mar 6 '18 at 8:31

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.