<p>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.</p>

<p><a href="https://craftcms.stackexchange.com/questions/21864/how-to-save-matrix-data-programmatically-in-craft-3/21865#21865">How to save Matrix data programmatically in Craft 3</a></p>

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

CC BY-SA 3.0
2018-03-06T08:31:58.420Z
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
CC BY-SA 3.0
2018-03-06T07:13:23.907Z
2018-03-06T07:13:23.907Z
Visky Máté
<p>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:</p>

<pre><code>$entry = new Entry();
$entry-&gt;sectionId = 10;
$entry-&gt;typeId = 1;
$entry-&gt;fieldLayoutId = $entry-&gt;getType()-&gt;fieldLayoutId;
$entry-&gt;authorId = 5;
$entry-&gt;enabled = true;
$entry-&gt;title = "Hello World!";
$entry-&gt;setFieldValues([
    'body' =&gt; "&lt;p&gt;I can’t believe I literally just called this “Hello World!”.&lt;/p&gt;",
]);
$success = Craft::$app-&gt;elements-&gt;saveElement($entry);
if (!$success) {
    Craft::error('Couldn’t save the entry "'.$entry-&gt;title.'"', __METHOD__);
}
</code></pre>

<p>reference: <a href="https://docs.craftcms.com/api/v3/craft-services-elements.html#saveElement()-detail" rel="nofollow noreferrer">https://docs.craftcms.com/api/v3/craft-services-elements.html#saveElement()-detail</a></p>

<p>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.</p>

CC BY-SA 3.0
2018-03-05T06:17:22.560Z
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
CC BY-SA 3.0
2018-03-05T06:26:12.597Z
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é
CC BY-SA 3.0
2018-03-05T06:49:48.883Z
You could copy the `EntriesController` and 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
CC BY-SA 3.0
2018-03-05T06:53:36.193Z
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é
CC BY-SA 3.0
2018-03-05T06:59:57.873Z
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
CC BY-SA 3.0
2018-03-05T07:02:32.950Z
Because I am new the the framework it was worth to ask is there any better nice clean way to do it. But thx for the hints appreciate it! The answer would be no supported builder or factory class alternatively we can copy the controller. I will review the code tomorrow and thx again!!!
Visky Máté
CC BY-SA 3.0
2018-03-06T04:33:55.167Z
Would just using a .csv file and something like the [import plugin](https://github.com/boboldehampsink/import) work? Might be easier in the long run.
Douglas McDonald
<entry>
How to create manually an Entry from command line
# How to create manually an Entry from command line

(Asked by: username_0 on 2018-03-05)

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' => "I can’t believe I literally just called this “Hello World!”.",
]);
$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.

**Tags:** entry
> **Comments:**
> **username_1**: 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  
> **username_0**: 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  
> **username_1**: You could copy the `EntriesController` and 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?  
> **username_0**: 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:)  
> **username_1**: 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  
> **username_0**: Because I am new the the framework it was worth to ask is there any better nice clean way to do it. But thx for the hints appreciate it! The answer would be no supported builder or factory class alternatively we can copy the controller. I will review the code tomorrow and thx again!!!  
> **username_2**: Would just using a .csv file and something like the [import plugin](https://github.com/boboldehampsink/import) work? Might be easier in the long run.  

---

## 1 answers

### Answer by username_0 on 2018-03-06. Score: 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](https://craftcms.stackexchange.com/questions/21864/how-to-save-matrix-data-programmatically-in-craft-3/21865#21865)

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

> **Comments:**
> **username_1**: 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)  

---

