<p>I am having some trouble when trying to create a new Entry in Craft3 via PHP. In Craft 2, I was able to do something like this:</p>

<pre><code>  $entry = new EntryModel();
  $entry-&gt;getContent()-&gt;title = $name;
  $entry-&gt;getContent()-&gt;answer = $answer;
  $handle = "nameOfTheSectionIWantToSaveIn";
  $entry-&gt;sectionId = craft()-&gt;sections-&gt;getSectionByHandle($handle)-&gt;id;
  $save = craft()-&gt;entries-&gt;saveEntry($entry);
</code></pre>

<p>Before asking, I took some time to read through the <a href="https://docs.craftcms.com/api/v3/" rel="nofollow noreferrer">Craft3 Class Reference</a> and came across the namespace:</p>

<pre><code>craft\elements
</code></pre>

<p>I have come to the conclusion(could be wrong) that I need to use something like the following:</p>

<pre><code>use craft\elements\Entry;
$entry = new Entry();
</code></pre>

<p>However, I am a little confused as to where I go from here. I checked the available <a href="https://docs.craftcms.com/api/v3/craft-elements-entry.html#public-methods" rel="nofollow noreferrer">Craft Entry Public Methods</a> and could not find a method like getContent(), which was used in Craft2. I did see a getContentTable(), but not sure if thats what I am looking for.</p>

CC BY-SA 4.0
2018-06-19T04:56:12.223Z
Please see this https://craftcms.stackexchange.com/questions/21864/how-to-save-matrix-data-programmatically-in-craft-3 instead of `getContent` you just do `setFieldValue`
Robin Schambach
CC BY-SA 4.0
2018-06-22T01:32:10.147Z
that example does not work for me. Instead of saving a matrix field, legit just tried to create an entry with one text field. It fails to save where I check to see if it errors by using $success = Craft::$app->elements->saveElement($entry); then checking if(!$success) { var_dump($success) } . All this returns to me is a bool(false), which doesnt give me much info about what happened.
Dan Zuzevich
CC BY-SA 4.0
2018-06-22T01:40:57.827Z
https://gist.github.com/DZuz14/b54b7266b0db39467bf265e0cec4559e
Dan Zuzevich
CC BY-SA 4.0
2018-06-22T05:06:37.920Z
It's impossible to say without your field layout but you can get the reason `$entry->getErrors()` like in Craft 2
Robin Schambach
CC BY-SA 4.0
2018-06-22T13:35:02.603Z
Thanks guy. By dumping getErrors() in the if conditional, it was able to tell me that my title cannot be blank. Another developer name had changed the default title field to be named, 'journalTitleShort', so I needed to do that inside of setFieldValues. It works.
Dan Zuzevich
<plugin-development><entries><craft3>
Create an Entry With PHP in CraftCMS3
# Create an Entry With PHP in CraftCMS3

(Asked by: username_0 on 2018-06-18)

I am having some trouble when trying to create a new Entry in Craft3 via PHP. In Craft 2, I was able to do something like this:
  $entry = new EntryModel();
  $entry->getContent()->title = $name;
  $entry->getContent()->answer = $answer;
  $handle = "nameOfTheSectionIWantToSaveIn";
  $entry->sectionId = craft()->sections->getSectionByHandle($handle)->id;
  $save = craft()->entries->saveEntry($entry);

Before asking, I took some time to read through the Craft3 Class Reference and came across the namespace:
craft\elements

I have come to the conclusion(could be wrong) that I need to use something like the following:
use craft\elements\Entry;
$entry = new Entry();

However, I am a little confused as to where I go from here. I checked the available Craft Entry Public Methods and could not find a method like getContent(), which was used in Craft2. I did see a getContentTable(), but not sure if thats what I am looking for.

**Tags:** plugin-development, entries, craft3
> **Comments:**
> **username_1**: Please see this https://craftcms.stackexchange.com/questions/21864/how-to-save-matrix-data-programmatically-in-craft-3 instead of `getContent` you just do `setFieldValue`  
> **username_0**: that example does not work for me. Instead of saving a matrix field, legit just tried to create an entry with one text field. It fails to save where I check to see if it errors by using $success = Craft::$app->elements->saveElement($entry); then checking if(!$success) { var_dump($success) } . All this returns to me is a bool(false), which doesnt give me much info about what happened.  
> **username_0**: https://gist.github.com/DZuz14/b54b7266b0db39467bf265e0cec4559e  
> **username_1**: It's impossible to say without your field layout but you can get the reason `$entry->getErrors()` like in Craft 2  
> **username_0**: Thanks guy. By dumping getErrors() in the if conditional, it was able to tell me that my title cannot be blank. Another developer name had changed the default title field to be named, 'journalTitleShort', so I needed to do that inside of setFieldValues. It works.  

---

## 0 answers

