<p>It turns out the data wasn't being saved as I hadn't set a <code>fieldLayoutId</code> value.</p>

<p>Now that I've figured this out, I've written a helper method to make saving any entry type much easier:</p>

<pre><code>public static function saveNewEntry(string $handle, array $fields) {

    $entryType = EntryType::find()-&gt;where(['handle' =&gt; $handle])-&gt;one();

    $entry = new Entry();    
    $entry-&gt;sectionId = $entryType-&gt;getAttribute('sectionId');         
    $entry-&gt;typeId = $entryType-&gt;getAttribute('id');      
    $entry-&gt;fieldLayoutId = $entryType-&gt;getAttribute('fieldLayoutId');     
    $entry-&gt;authorId = 1; 

    if(isset($fields['title'])) { 
        $entry-&gt;title = $fields['title'];     
        unset($fields['title']); 
    }

    if(isset($fields['slug'])) {
        $entry-&gt;slug = $fields['slug'];     
        unset($fields['slug']);
    }

    $entry-&gt;setFieldValues($fields);

    if(Craft::$app-&gt;elements-&gt;saveElement($entry)) {
        return $entry;
    } else {
        throw new \Exception("Couldn't save new bespoke product: " . print_r($entry-&gt;getErrors(), true)); 
    }
}
</code></pre>

<p>This can be used like so:</p>

<pre><code>return self::saveNewEntry('article', [
    'title' =&gt; $id,
    'slug' =&gt; $id,
    'excerpt' =&gt; $excerpt,
    'body' =&gt; $body
]);
</code></pre>

CC BY-SA 4.0
2019-11-19T02:19:22.290Z
Do you know how to save fields of Entries type?
biko
CC BY-SA 3.0
2017-06-26T22:20:29.230Z
2017-06-26T22:20:29.230Z
SteveEdson
<p>I'm trying to create a new Entry via PHP, with custom fields (Craft 3). I can save it, however my custom fields don't appear. I've tried using <code>setFieldValue</code>, as well as <code>$entry-&gt;excerpt = 'Lorem...';</code>, however neither work.</p>

<p>My code is below, using <code>craft\elements\Entry</code>:</p>

<pre><code>$entry = new Entry();
$entry-&gt;sectionId = 4;
$entry-&gt;typeId = 4;
$entry-&gt;authorId = 1;
$id = uniqid('product_');
$entry-&gt;title = $id;
$entry-&gt;slug = $id;

$entry-&gt;setFieldValue('excerpt', 'Lorem...');

if(Craft::$app-&gt;elements-&gt;saveElement($entry)) {
    return $entry;
} else {
    throw new \Exception("Couldn't save new bespoke product: " . print_r($entry-&gt;getErrors(), true));
}
</code></pre>

CC BY-SA 3.0
2017-06-26T21:33:44.063Z
I see you've got this sorted via: https://github.com/craftcms/cms/issues/1797 Would you mind adding that as an official answer here in case it helps someone in the future?
Brad Bell
CC BY-SA 3.0
2017-06-26T22:20:37.440Z
No problem, done
SteveEdson
<entry><custom-field><craft3><saveentry>
How to save a new entry with custom fields - Craft 3
# How to save a new entry with custom fields - Craft 3

(Asked by: username_0 on 2017-06-25)

I'm trying to create a new Entry via PHP, with custom fields (Craft 3). I can save it, however my custom fields don't appear. I've tried using setFieldValue, as well as $entry->excerpt = 'Lorem...';, however neither work.
My code is below, using craft\elements\Entry:
$entry = new Entry();
$entry->sectionId = 4;
$entry->typeId = 4;
$entry->authorId = 1;
$id = uniqid('product_');
$entry->title = $id;
$entry->slug = $id;

$entry->setFieldValue('excerpt', 'Lorem...');

if(Craft::$app->elements->saveElement($entry)) {
    return $entry;
} else {
    throw new \Exception("Couldn't save new bespoke product: " . print_r($entry->getErrors(), true));
}

**Tags:** entry, custom-field, craft3, saveentry

> **Comments:**
> **username_1**: I see you've got this sorted via: https://github.com/craftcms/cms/issues/1797 Would you mind adding that as an official answer here in case it helps someone in the future?  
> **username_0**: No problem, done  

---

## 1 answers

### Answer by username_0 on 2017-06-26. Score: 4

It turns out the data wasn't being saved as I hadn't set a fieldLayoutId value.
Now that I've figured this out, I've written a helper method to make saving any entry type much easier:
public static function saveNewEntry(string $handle, array $fields) {

    $entryType = EntryType::find()->where(['handle' => $handle])->one();

    $entry = new Entry();    
    $entry->sectionId = $entryType->getAttribute('sectionId');         
    $entry->typeId = $entryType->getAttribute('id');      
    $entry->fieldLayoutId = $entryType->getAttribute('fieldLayoutId');     
    $entry->authorId = 1; 

    if(isset($fields['title'])) { 
        $entry->title = $fields['title'];     
        unset($fields['title']); 
    }

    if(isset($fields['slug'])) {
        $entry->slug = $fields['slug'];     
        unset($fields['slug']);
    }

    $entry->setFieldValues($fields);

    if(Craft::$app->elements->saveElement($entry)) {
        return $entry;
    } else {
        throw new \Exception("Couldn't save new bespoke product: " . print_r($entry->getErrors(), true)); 
    }
}

This can be used like so:
return self::saveNewEntry('article', [
    'title' => $id,
    'slug' => $id,
    'excerpt' => $excerpt,
    'body' => $body
]);

> **Comments:**
> **username_2**: Do you know how to save fields of Entries type?  

---

