<p>In Craft's v3 API documentation they give an example:</p>

<p><a href="https://docs.craftcms.com/api/v3/craft-services-elements.html#method-saveelement" rel="nofollow noreferrer">https://docs.craftcms.com/api/v3/craft-services-elements.html#method-saveelement</a></p>

<pre><code>$entry = new Entry();
$entry-&gt;sectionId = 10;
$entry-&gt;typeId = 1;
$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>

CC BY-SA 4.0
2019-05-15T13:05:06.983Z
Just FYI, this is not the question I was asking above if you will notice. I was asking how to prevent re-triggering the onBeforeSave and onAfterSave if I create entries programmatically.
Alex Glover
CC BY-SA 4.0
2018-10-25T19:19:26.673Z
2018-10-25T19:19:26.673Z
danbrellis
<p>I am currently trying to save a new entry with <code>craft()-&gt;elements-&gt;saveElement()</code>, as I want to avoid triggering the <code>onBeforeSave</code> and <code>onAfterSave</code> events.</p>

<p>When I run my script, it returns a successful entry id, but there is no entry in the admin.</p>

<p>Here is my code:</p>

<pre><code>$entry = new EntryModel();
$entry-&gt;sectionId = $sectionId;
$entry-&gt;typeId = $entryTypeId;
$entry-&gt;enabled = true;
$entry-&gt;authorId = 1;

$success = craft()-&gt;elements-&gt;saveElement($entry, false);

if ($success) {
    echo $entry-&gt;id;
}
</code></pre>

CC BY-SA 3.0
2018-03-07T03:14:41.077Z
Not an answer, but check out Craft's own `craft/app/controllers/EntriesController->saveEntry()` to see how an EntryModel is created and populated and passed off to the ElementsService.
Brad Bell
CC BY-SA 3.0
2018-03-07T20:38:46.870Z
Thanks! I've done a little digging there but haven't quite figured out what might be missing. I'll keep digging.
Alex Glover
CC BY-SA 3.0
2018-03-08T14:22:36.317Z
@BradBell I ended up copying the `saveEntry` method from the `EntriesService` and ripping out anything event related.

Additionally, I think it'd be amazing if there were a way to call saveEntry and disable any events, although I don't know if that would ever be possible.
Alex Glover
CC BY-SA 3.0
2018-03-08T22:44:02.367Z
You can do that by simply create a new instance of the entries service I noticed that by mistake https://craftcms.stackexchange.com/questions/19334/entries-onsaveentry-does-not-fire-from-frontend
Robin Schambach
<entrymodel>
Programmatically create new entry with saveElement
# Programmatically create new entry with saveElement

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

I am currently trying to save a new entry with craft()->elements->saveElement(), as I want to avoid triggering the onBeforeSave and onAfterSave events.
When I run my script, it returns a successful entry id, but there is no entry in the admin.
Here is my code:
$entry = new EntryModel();
$entry->sectionId = $sectionId;
$entry->typeId = $entryTypeId;
$entry->enabled = true;
$entry->authorId = 1;

$success = craft()->elements->saveElement($entry, false);

if ($success) {
    echo $entry->id;
}

**Tags:** entrymodel

> **Comments:**
> **username_1**: Not an answer, but check out Craft's own `craft/app/controllers/EntriesController->saveEntry()` to see how an EntryModel is created and populated and passed off to the ElementsService.  
> **username_0**: Thanks! I've done a little digging there but haven't quite figured out what might be missing. I'll keep digging.  
> **username_0**: @BradBell I ended up copying the `saveEntry` method from the `EntriesService` and ripping out anything event related.  Additionally, I think it'd be amazing if there were a way to call saveEntry and disable any events, although I don't know if that would ever be possible.  
> **username_2**: You can do that by simply create a new instance of the entries service I noticed that by mistake https://craftcms.stackexchange.com/questions/19334/entries-onsaveentry-does-not-fire-from-frontend  

---

## 1 answers

### Answer by username_3 on 2018-10-25. Score: 3

In Craft's v3 API documentation they give an example:
https://docs.craftcms.com/api/v3/craft-services-elements.html#method-saveelement
$entry = new Entry();
$entry->sectionId = 10;
$entry->typeId = 1;
$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__);
}

> **Comments:**
> **username_0**: Just FYI, this is not the question I was asking above if you will notice. I was asking how to prevent re-triggering the onBeforeSave and onAfterSave if I create entries programmatically.  

---

