Answers.Body: <p>Just so there's an answer here. OP was using Craft 2 code in a Craft 3 environment.</p>
  
  <p>The code should be something like this in Craft 3:</p>
  
  <pre class="lang-php prettyprint-override"><code>$entry = new Entry();
  $entry-&gt;sectionId = $yourSectionId;
  $entry-&gt;typeId = $yourEntryTypeId;
  $entry-&gt;authorId = $yourAuthorId;
  $entry-&gt;enabled = true;
  $entry-&gt;title = "Foo";
  $entry-&gt;setFieldValues([
      'field1' =&gt; "Foo",
      'field2' =&gt; "Bar",
  ]);
  $success = Craft::$app-&gt;elements-&gt;saveElement($entry);
  if (!$success) {
      Craft::error('Couldn’t save the entry "'.$entry-&gt;title.'"', __METHOD__);
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-01T11:02:28.620Z
Answers.Text: still not solving my problem.First line is showing error.How can you import the Entry class correctly?
Answers.UserDisplayName: jamesSeb
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-01T11:04:38.150Z
Answers.Text: I have editeed my qn for better clarity
Answers.UserDisplayName: jamesSeb
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-02T20:27:35.760Z
Answers.Text: Hi, sorry I just saw this again. I think my previous comment on how to import (`use`) PHP classes in your code covers that. If you'd like to read more on that, [the PHP docs](https://www.php.net/manual/en/language.namespaces.importing.php) are worth a look. You could do something like `use craft\elements\Entry as MySpecificUseOfAnEntry;` and then when you want to new up that class do `$entry = new MySpecificUseOfAnEntry();` and the code above, with those changes, will work fine.
Answers.UserDisplayName: billythekid
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-01T10:56:53.193Z
Answers.LastActivityDate: 2020-04-01T10:56:53.193Z
Answers.UserDisplayName: billythekid
Answers.Body: <p>Either use</p>
  
  <pre><code>use craft\elements\Entry;
  $entry = new Entry();
  </code></pre>
  
  <p>OR </p>
  
  <pre><code>$entry = new \craft\elements\Entry();
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-02T05:06:44.697Z
Answers.LastActivityDate: 2020-04-02T05:06:44.697Z
Answers.UserDisplayName: jamesSeb
Answers.Body: <p>You can download and see guest-entries plugin code to correctly see how to save an entry from a plugin.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-02T05:25:58.087Z
Answers.LastActivityDate: 2020-04-02T05:25:58.087Z
Answers.UserDisplayName: jamesSeb
Body: <p>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 <code>namespace putyourlightson\userguide\controllers;</code>.I copied this code from StackExchange. My first line is getting an error(<code>Class 'Craft\EntryModel' not found</code>).</p>
  
  <pre><code>$entry = new \Craft\EntryModel();
  
  $attributes = [
    'section'   =&gt; $yourSectionId,
    'entryType' =&gt; $yourEntryTypeId,
    'field1'    =&gt; 'foo',
    'field2'    =&gt; 'bar'
  ];
  
  $entry-&gt;getContent()-&gt;setAttributes($attributes);
  
  craft()-&gt;entries()-&gt;saveEntry($entry);
  </code></pre>
  
  <p>How should you import it?<a href="https://i.sstatic.net/TRygn.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/TRygn.png" alt="enter image description here"></a></p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-04-01T10:03:35.643Z
Comments.Text: Do you mean add a new section model, or a new entry type maybe? (Because line one in your code is creating a new entry model)
  If you're just wondering how to save an entry as it looks like above, [check the docs here](https://docs.craftcms.com/api/v2/craft-entriesservice.html#method-saveentry)
Comments.UserDisplayName: billythekid
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-04-01T10:17:38.513Z
Comments.Text: ok I was not clear ...1st line is not working for me.my question should have been how do you import EntryModel
Comments.UserDisplayName: jamesSeb
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-04-01T10:31:04.113Z
Comments.Text: The EntryModel class is under the `Craft` namespace. If your plugin isn't under the `Craft` namespace (back in Craft 2 most plugins _were_ as the examples were all like that) you'll need to `use` it at the top of the plugin/class where your code is. so `use Craft\EntryModel;` should do it. Or namespace the EntryModel right there `$entry = new Craft\EntryModel();`
Comments.UserDisplayName: billythekid
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-04-01T10:35:36.863Z
Comments.Text: but still it is showing ERROR -Class 'Craft\EntryModel' not found
Comments.UserDisplayName: jamesSeb
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-04-01T10:37:23.210Z
Comments.Text: I tried to use $entry = new \Craft\EntryModel(); as well,it is also not working
Comments.UserDisplayName: jamesSeb
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-04-01T10:51:56.443Z
Comments.Text: Ahh, you're trying to use Craft 2 code in Craft 3. You've just looked at the wrong docs. Try [this link](https://docs.craftcms.com/api/v3/craft-services-elements.html#method-saveelement) for the Craft 3 version. (Most things have dropped "Model" from their names so you want `new Entry();`)
Comments.UserDisplayName: billythekid
Tags: <plugin-development><entry><entrymodel><saveentry>
Title: How to create a new Entry model in Craft 3?
ThreadText: # How to create a new Entry model in Craft 3?
  
  (Asked by: username_0 on 2020-04-01)
  
  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);
  
  ```
  
  How should you import it?[![enter image description here](https://i.sstatic.net/TRygn.png)](https://i.sstatic.net/TRygn.png)
  
  **Tags:** plugin-development, entry, entrymodel, saveentry
  
  > **Comments:**
  > **username_1**: Do you mean add a new section model, or a new entry type maybe? (Because line one in your code is creating a new entry model) If you're just wondering how to save an entry as it looks like above, [check the docs here](https://docs.craftcms.com/api/v2/craft-entriesservice.html#method-saveentry)  
  > **username_0**: ok I was not clear ...1st line is not working for me.my question should have been how do you import EntryModel  
  > **username_1**: The EntryModel class is under the `Craft` namespace. If your plugin isn't under the `Craft` namespace (back in Craft 2 most plugins _were_ as the examples were all like that) you'll need to `use` it at the top of the plugin/class where your code is. so `use Craft\EntryModel;` should do it. Or namespace the EntryModel right there `$entry = new Craft\EntryModel();`  
  > **username_0**: but still it is showing ERROR -Class 'Craft\EntryModel' not found  
  > **username_0**: I tried to use $entry = new \Craft\EntryModel(); as well,it is also not working  
  > **username_1**: Ahh, you're trying to use Craft 2 code in Craft 3. You've just looked at the wrong docs. Try [this link](https://docs.craftcms.com/api/v3/craft-services-elements.html#method-saveelement) for the Craft 3 version. (Most things have dropped "Model" from their names so you want `new Entry();`)  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2020-04-01. Score: 4
  
  Just so there's an answer here. OP was using Craft 2 code in a Craft 3 environment.
  
  The code should be something like this in Craft 3:
  
  ```php
  $entry = new Entry();
  $entry->sectionId = $yourSectionId;
  $entry->typeId = $yourEntryTypeId;
  $entry->authorId = $yourAuthorId;
  $entry->enabled = true;
  $entry->title = "Foo";
  $entry->setFieldValues([
      'field1' => "Foo",
      'field2' => "Bar",
  ]);
  $success = Craft::$app->elements->saveElement($entry);
  if (!$success) {
      Craft::error('Couldn’t save the entry "'.$entry->title.'"', __METHOD__);
  }
  
  ```
  
  > **Comments:**
  > **username_0**: still not solving my problem.First line is showing error.How can you import the Entry class correctly?  
  > **username_0**: I have editeed my qn for better clarity  
  > **username_1**: Hi, sorry I just saw this again. I think my previous comment on how to import (`use`) PHP classes in your code covers that. If you'd like to read more on that, [the PHP docs](https://www.php.net/manual/en/language.namespaces.importing.php) are worth a look. You could do something like `use craft\elements\Entry as MySpecificUseOfAnEntry;` and then when you want to new up that class do `$entry = new MySpecificUseOfAnEntry();` and the code above, with those changes, will work fine.  
  
  ---
  
  ### Answer by username_0 on 2020-04-02. Score: 1
  
  Either use
  
  ```
  use craft\elements\Entry;
  $entry = new Entry();
  
  ```
  
  OR
  
  ```
  $entry = new \craft\elements\Entry();
  
  ```
  
  
  ---
  
  ### Answer by username_0 on 2020-04-02. Score: 1
  
  You can download and see guest-entries plugin code to correctly see how to save an entry from a plugin.
  
  
  ---
  
  