<p>As per the docs at <a href="https://craftcms.com/docs/3.x/extend/element-types.html#saving-custom-field-values" rel="nofollow noreferrer">https://craftcms.com/docs/3.x/extend/element-types.html#saving-custom-field-values</a></p>
<blockquote>
<p>When saving values on a custom field, always use the <code>setFieldValue()</code> or <code>setFieldValues()</code> methods rather than directly accessing the field handle as a property on the element object. This ensures the value is normalized and marked as dirty for delta saves.</p>
</blockquote>
<pre class="lang-php prettyprint-override"><code>$entry-&gt;setFieldValue('testField', 'foo');
$entry-&gt;setFieldValue('entriesField', [$relatedEntryId1, $relatedEntryId2]);

Craft::$app-&gt;elements-&gt;saveElement($entry);
</code></pre>

CC BY-SA 4.0
2021-09-15T07:56:37.957Z
Thanks.  Yes this works with the latest Craft versions but unsure that it was documented in the past.  Answer edited.
biko
CC BY-SA 4.0
2021-04-28T18:59:58.157Z
2021-09-15T15:11:43.403Z
2021-09-15T15:11:43.403Z
Ben Croker
<p>Ok this was easy.  Tried it at first but didn't work and was successful after another go.  All it needed was a array of <code>Entry IDs</code>.</p>
<p>Working code:</p>
<pre><code>$entry = Entry::find()-&gt;id($entryId)-&gt;one();
$entry-&gt;setFieldValue('textField', 20);
$entry-&gt;setFieldValue('entriesField', [$relatedEntryId1, $relatedEntryId2]);

Craft::$app-&gt;elements-&gt;saveElement($entry);
</code></pre>

CC BY-SA 4.0
2019-11-19T02:33:09.140Z
2021-09-15T07:55:18.720Z
2021-09-15T07:55:18.720Z
biko
<p>Using PHP, I want to update a field of <code>Entries</code> type of an existing entry.</p>

<p>Fields of type <code>Text</code> and <code>Number</code> will update with the following code:</p>

<pre><code>$entry = Entry::find()-&gt;id($entryId)-&gt;one();
$entry-&gt;textField = 20; // works
$entry-&gt;entriesField = Entry::find()-&gt;id($relatedEntryIds)-&gt;all(); // doesn't work
Craft::$app-&gt;elements-&gt;saveElement($entry);
</code></pre>

<p>I looked into the source code and it seems to be expecting a <code>craft\elements\db\EntryQuery</code> object but I can't get it to work.</p>

<p>Can anyone please help?</p>

<plugin-development><craft3><php><elementquery><element>
Craft 3 plugin - how to update entry with field of Entries type
# Craft 3 plugin - how to update entry with field of Entries type

(Asked by: username_0 on 2019-11-19)

Using PHP, I want to update a field of Entries type of an existing entry.
Fields of type Text and Number will update with the following code:
$entry = Entry::find()->id($entryId)->one();
$entry->textField = 20; // works
$entry->entriesField = Entry::find()->id($relatedEntryIds)->all(); // doesn't work
Craft::$app->elements->saveElement($entry);

I looked into the source code and it seems to be expecting a craft\elements\db\EntryQuery object but I can't get it to work.
Can anyone please help?

**Tags:** plugin-development, craft3, php, elementquery, element


---

## 2 answers

### Answer by username_0 on 2019-11-19. Score: 5 | Accepted answer

Ok this was easy.  Tried it at first but didn't work and was successful after another go.  All it needed was a array of Entry IDs.
Working code:
$entry = Entry::find()->id($entryId)->one();
$entry->setFieldValue('textField', 20);
$entry->setFieldValue('entriesField', [$relatedEntryId1, $relatedEntryId2]);

Craft::$app->elements->saveElement($entry);


---

### Answer by username_1 on 2021-04-28. Score: 3

As per the docs at https://craftcms.com/docs/3.x/extend/element-types.html#saving-custom-field-values

When saving values on a custom field, always use the setFieldValue() or setFieldValues() methods rather than directly accessing the field handle as a property on the element object. This ensures the value is normalized and marked as dirty for delta saves.

$entry->setFieldValue('testField', 'foo');
$entry->setFieldValue('entriesField', [$relatedEntryId1, $relatedEntryId2]);

Craft::$app->elements->saveElement($entry);

> **Comments:**
> **username_0**: Thanks.  Yes this works with the latest Craft versions but unsure that it was documented in the past.  Answer edited.  

---

