Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-09-15T07:56:37.957Z
Answers.Text: Thanks.  Yes this works with the latest Craft versions but unsure that it was documented in the past.  Answer edited.
Answers.UserDisplayName: biko
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-04-28T18:59:58.157Z
Answers.LastActivityDate: 2021-09-15T15:11:43.403Z
Answers.LastEditDate: 2021-09-15T15:11:43.403Z
Answers.UserDisplayName: Ben Croker
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-19T02:33:09.140Z
Answers.LastActivityDate: 2021-09-15T07:55:18.720Z
Answers.LastEditDate: 2021-09-15T07:55:18.720Z
Answers.UserDisplayName: biko
Body: <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>
  
Tags: <plugin-development><craft3><php><elementquery><element>
Title: Craft 3 plugin - how to update entry with field of Entries type
ThreadText: # 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.
  
  ```php
  $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.  
  
  ---
  
  