Answers.Body: <p>If you are running on Craft 3, as of 3.4.0 (and really since the release of craft 3), setting custom field values directly like</p>
  
  <pre><code>$entry-&gt;customField = [id1, id2, id3];
  </code></pre>
  
  <p>is broken. You should instead use the setFieldValue or setFieldValues methods.</p>
  
  <p>in your case, a relationship field can be set like so:</p>
  
  <pre><code>$reglages-&gt;setFieldValue(curatedHomepageArticles, [&lt;ARRAY OF ENTRY IDs]);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-31T11:40:48.443Z
Answers.Text: This is a good answer. I marked @oli's post as the official answer for this because I also had a problem with the way I was saving the Global set fields. I should use `Craft::$app->elements->saveElement($reglages);`
Answers.UserDisplayName: Gfra54
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-30T16:11:57.070Z
Answers.LastActivityDate: 2020-01-30T16:11:57.070Z
Answers.UserDisplayName: Nick
Answers.Body: <p>Although this response is specific to assets the same principles should apply to any field based on relationships (eg categories, tags, entries, etc): </p>
  
  <p><a href="https://craftcms.stackexchange.com/a/28499/992">https://craftcms.stackexchange.com/a/28499/992</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-29T09:25:48.570Z
Answers.Text: Thank you. I found this thread already and I am not sure why, but it doesn't work. Either the saveSet is not working, or the line juste before. I will make some more tests.
Answers.UserDisplayName: Gfra54
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-28T18:55:55.860Z
Answers.LastActivityDate: 2020-01-28T18:55:55.860Z
Answers.UserDisplayName: Cole Henley
Answers.Body: <p>If you use <code>Craft::$app-&gt;getGlobals()-&gt;saveSet($reglages);</code> you are saving the Global's itself (its settings), not its content. </p>
  
  <p>Here, you should use <code>Craft::$app-&gt;elements-&gt;saveElement($reglages);</code> See below:</p>
  
  <pre><code>$reglages = \craft\elements\GlobalSet::find()
              -&gt;handle('reglages')
              -&gt;one();
  
  $entries = [$entry-&gt;id];
  
  foreach($reglages-&gt;curatedhomepagearticles-&gt;all() as $tmpEntry)  {
      $entries[] = $tmpEntry-&gt;id;
  }
  
  $entries = array_unique($entries);
  $entries = array_slice($entries, 0, 10);
  
  $reglages-&gt;setFieldValue('curatedhomepagearticles',$entries);
  
  Craft::$app-&gt;elements-&gt;saveElement($reglages);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-31T10:26:07.247Z
Answers.LastActivityDate: 2020-01-31T10:26:07.247Z
Answers.UserDisplayName: Oli
Body: <p>I work with Craft 3.</p>
  
  <p>I have an <a href="https://docs.craftcms.com/v3/entries-fields.html" rel="nofollow noreferrer">Entries field</a> in a global set, I am able to get the global set, and then "read" this field programmatically (in a plugin) to fetch the entries that where added to it by looping on the <code>EntryQuery</code> data.</p>
  
  <p>I want to be able to add another entry to this field via a php function in my plugin (so not in a Twig template), and then save the it, in the global set. The documentation is not very concrete on this part.
  Thanks</p>
  
  <p>here is my code : </p>
  
  <pre><code>            $reglages = \craft\elements\GlobalSet::find()
              -&gt;handle('reglages')
              -&gt;one();
  
              $entries = [$entry-&gt;id];
  
              foreach($reglages-&gt;curatedHomepageArticles as $tmpEntry)  {
                  $entries[] = $tmpEntry-&gt;id;
              }
              $entries = array_unique($entries);
              $entries = array_slice($entries, 0, 10);
  
              $reglages-&gt;curatedHomepageArticles = $entries;
  
              $return = Craft::$app-&gt;getGlobals()-&gt;saveSet($reglages);
              var_dump($return);
  </code></pre>
  
  <p>The point is to add the <code>$entry</code> to the entry field called <code>curatedHomepageArticles</code> with no duplicates. I am able to read the entries that are already in the entry field, and then build the array of ids called <code>$entries</code> but nothing happens when I try to update the field with a new list of entries id <code>$entries</code>. <code>$return</code> is <code>true</code> and there are no exceptions thrown. Can you help me understand what is going on here ?</p>
  
Tags: <craft3><php>
Title: Programmatically add entries to an Entry field (craft\fields\Entries)
ThreadText: # Programmatically add entries to an Entry field (craft\fields\Entries)
  
  (Asked by: username_0 on 2020-01-27)
  
  I work with Craft 3.
  
  I have an [Entries field](https://docs.craftcms.com/v3/entries-fields.html) in a global set, I am able to get the global set, and then "read" this field programmatically (in a plugin) to fetch the entries that where added to it by looping on the `EntryQuery` data.
  
  I want to be able to add another entry to this field via a php function in my plugin (so not in a Twig template), and then save the it, in the global set. The documentation is not very concrete on this part.
  Thanks
  
  here is my code :
  
  ```
              $reglages = \craft\elements\GlobalSet::find()
              ->handle('reglages')
              ->one();
  
              $entries = [$entry->id];
  
              foreach($reglages->curatedHomepageArticles as $tmpEntry)  {
                  $entries[] = $tmpEntry->id;
              }
              $entries = array_unique($entries);
              $entries = array_slice($entries, 0, 10);
  
              $reglages->curatedHomepageArticles = $entries;
  
              $return = Craft::$app->getGlobals()->saveSet($reglages);
              var_dump($return);
  
  ```
  
  The point is to add the `$entry` to the entry field called `curatedHomepageArticles` with no duplicates. I am able to read the entries that are already in the entry field, and then build the array of ids called `$entries` but nothing happens when I try to update the field with a new list of entries id `$entries`. `$return` is `true` and there are no exceptions thrown. Can you help me understand what is going on here ?
  
  **Tags:** craft3, php
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_3 on 2020-01-31. Score: 4 | Accepted answer
  
  If you use `Craft::$app->getGlobals()->saveSet($reglages);` you are saving the Global's itself (its settings), not its content.
  
  Here, you should use `Craft::$app->elements->saveElement($reglages);` See below:
  
  ```
  $reglages = \craft\elements\GlobalSet::find()
              ->handle('reglages')
              ->one();
  
  $entries = [$entry->id];
  
  foreach($reglages->curatedhomepagearticles->all() as $tmpEntry)  {
      $entries[] = $tmpEntry->id;
  }
  
  $entries = array_unique($entries);
  $entries = array_slice($entries, 0, 10);
  
  $reglages->setFieldValue('curatedhomepagearticles',$entries);
  
  Craft::$app->elements->saveElement($reglages);
  
  ```
  
  
  ---
  
  ### Answer by username_1 on 2020-01-30. Score: 4
  
  If you are running on Craft 3, as of 3.4.0 (and really since the release of craft 3), setting custom field values directly like
  
  ```
  $entry->customField = [id1, id2, id3];
  
  ```
  
  is broken. You should instead use the setFieldValue or setFieldValues methods.
  
  in your case, a relationship field can be set like so:
  
  ```
  $reglages->setFieldValue(curatedHomepageArticles, [<ARRAY OF ENTRY IDs]);
  
  ```
  
  > **Comments:**
  > **username_0**: This is a good answer. I marked @oli's post as the official answer for this because I also had a problem with the way I was saving the Global set fields. I should use `Craft::$app->elements->saveElement($reglages);`  
  
  ---
  
  ### Answer by username_2 on 2020-01-28. Score: 1
  
  Although this response is specific to assets the same principles should apply to any field based on relationships (eg categories, tags, entries, etc):
  
  <https://craftcms.stackexchange.com/a/28499/992>
  
  > **Comments:**
  > **username_0**: Thank you. I found this thread already and I am not sure why, but it doesn't work. Either the saveSet is not working, or the line juste before. I will make some more tests.  
  
  ---
  
  