Answers.Body: <p>I got around this issue by looping through all fields that were of BaseElementFieldType re-setting them to their current value.</p>
  
  <pre class="lang-php prettyprint-override"><code>$fields = $entry-&gt;getFieldLayout()-&gt;getFields();
  
  $referentialFields = array();
  foreach ($fields as $layoutField) {
    $field = $layoutField-&gt;getField();
    if ($field-&gt;getFieldType() instanceof BaseElementFieldType) {
      $referentialFields[$field-&gt;handle] = $entry-&gt;{$field-&gt;handle}-&gt;ids();
    }
  }
  
  $entry-&gt;setContentFromPost(array_merge($referentialFields, array(
    'yourField' =&gt; 'yourValue'
  )));
  
  craft()-&gt;entries-&gt;saveEntry($entry);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-14T03:29:19.220Z
Answers.LastActivityDate: 2016-01-14T03:29:19.220Z
Answers.UserDisplayName: Brian Mathews
Answers.Body: <p>You're very close. Try this:</p>
  
  <pre><code>$entry-&gt;getContent()-&gt;setAttribute('metaStatus', 'Declined');
  craft()-&gt;entries-&gt;saveEntry($entry);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-29T16:40:01.907Z
Answers.Text: Unfortunately it still fails validation – required fields still complain.
Answers.UserDisplayName: Jon
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-30T07:49:16.383Z
Answers.Text: But is this an entry that has already been saved and has validated? Or is it a new entry?
Answers.UserDisplayName: André Elvan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-30T17:01:19.253Z
Answers.Text: It's already been saved, I'm retrieving it like so:  `$entry = craft()->entries->getEntryById($entryId);`. All of the other fields except for the matrix/asset/entry fields are pre-filled with the `getContent` method so I assume this is just a Craft bug...
Answers.UserDisplayName: Jon
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-29T07:59:50.860Z
Answers.LastActivityDate: 2015-09-29T07:59:50.860Z
Answers.UserDisplayName: André Elvan
Answers.Body: <p><strong>Craft 3</strong></p>
  <pre class="lang-php prettyprint-override"><code>$entry-&gt;setFieldValue('my_field_handle', 'new_value');
  $app-&gt;elements-&gt;saveElement($entry);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-30T17:02:14.450Z
Answers.LastActivityDate: 2020-10-30T17:02:14.450Z
Answers.UserDisplayName: Simon Epskamp
Answers.Body: <p>I'm not completely sure of the technical reasons (I guess something to do with relation fields not have a column in the <code>craft_content</code> table), but you also need to re-save the existing ids for any relation fields (Assets, Entries, Categories, Matrix, etc) before the element can be saved:</p>
  
  <pre><code>$entry-&gt;setContentFromPost(array(
      'metaStatus'  =&gt; 'Declined',
      'assetsField' =&gt; $entry-&gt;assetsField-&gt;ids(),
      'matrixField' =&gt; $entry-&gt;matrixField-&gt;ids(),
  );
  
  craft()-&gt;entries-&gt;saveEntry($entry);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-14T01:29:34.887Z
Answers.LastActivityDate: 2016-01-14T01:29:34.887Z
Answers.UserDisplayName: Mike Pepper
Body: <p>I'm trying to update an entry's field value from a Craft plugin. First I was trying to just update the attributes of the entry like so:</p>
  
  <pre><code>$entry-&gt;getContent()-&gt;setAttributes(array(
    'metaStatus' =&gt; 'Declined'
  ));
  
  craft()-&gt;entries-&gt;saveEntry($entry);
  </code></pre>
  
  <p>But that wouldn't work because <code>getContent</code> doesn't return the data needed in order for the <code>EntryModel</code> to pass validation. The next solution was to loop through each of the fields and grab their field value like so:</p>
  
  <pre><code>$fields = array();
  
  foreach($entry-&gt;getFieldLayout()-&gt;getFields() as $layout) {
    $handle = $layout-&gt;getField()-&gt;handle;
  
    $fields[$handle] = $entry-&gt;getFieldValue($handle);
  }
  
  $fields['metaStatus'] = 'Declined';
  
  $entry-&gt;setContent($fields);
  
  craft()-&gt;entries-&gt;saveEntry($entry); // FAIL
  </code></pre>
  
  <p><em>Now</em> I'm having issues with matrix/asset fields validating since they come back as empty. Am I going to have to look for matrix/asset fields in my loop above and correctly format the data? There must be an easier way to do this.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-01-14T08:04:53.407Z
Comments.Text: Related question answered by Brandon Kelly: [Categories field is losing its value when programmatically updating other fields’ values](http://craftcms.stackexchange.com/a/11146/125)
Comments.UserDisplayName: carlcs
Tags: <entry><entries-field><validation>
Title: Programmatically update entry field
ThreadText: # Programmatically update entry field
  
  (Asked by: username_0 on 2015-09-28)
  
  I'm trying to update an entry's field value from a Craft plugin. First I was trying to just update the attributes of the entry like so:
  
  ```
  $entry->getContent()->setAttributes(array(
    'metaStatus' => 'Declined'
  ));
  
  craft()->entries->saveEntry($entry);
  
  ```
  
  But that wouldn't work because `getContent` doesn't return the data needed in order for the `EntryModel` to pass validation. The next solution was to loop through each of the fields and grab their field value like so:
  
  ```
  $fields = array();
  
  foreach($entry->getFieldLayout()->getFields() as $layout) {
    $handle = $layout->getField()->handle;
  
    $fields[$handle] = $entry->getFieldValue($handle);
  }
  
  $fields['metaStatus'] = 'Declined';
  
  $entry->setContent($fields);
  
  craft()->entries->saveEntry($entry); // FAIL
  
  ```
  
  *Now* I'm having issues with matrix/asset fields validating since they come back as empty. Am I going to have to look for matrix/asset fields in my loop above and correctly format the data? There must be an easier way to do this.
  
  **Tags:** entry, entries-field, validation
  
  > **Comments:**
  > **username_1**: Related question answered by Brandon Kelly: [Categories field is losing its value when programmatically updating other fields’ values](http://craftcms.stackexchange.com/a/11146/125)  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_2 on 2016-01-14. Score: 6
  
  I got around this issue by looping through all fields that were of BaseElementFieldType re-setting them to their current value.
  
  ```php
  $fields = $entry->getFieldLayout()->getFields();
  
  $referentialFields = array();
  foreach ($fields as $layoutField) {
    $field = $layoutField->getField();
    if ($field->getFieldType() instanceof BaseElementFieldType) {
      $referentialFields[$field->handle] = $entry->{$field->handle}->ids();
    }
  }
  
  $entry->setContentFromPost(array_merge($referentialFields, array(
    'yourField' => 'yourValue'
  )));
  
  craft()->entries->saveEntry($entry);
  
  ```
  
  
  ---
  
  ### Answer by username_3 on 2015-09-29. Score: 4
  
  You're very close. Try this:
  
  ```
  $entry->getContent()->setAttribute('metaStatus', 'Declined');
  craft()->entries->saveEntry($entry);
  
  ```
  
  > **Comments:**
  > **username_0**: Unfortunately it still fails validation – required fields still complain.  
  > **username_3**: But is this an entry that has already been saved and has validated? Or is it a new entry?  
  > **username_0**: It's already been saved, I'm retrieving it like so:  `$entry = craft()->entries->getEntryById($entryId);`. All of the other fields except for the matrix/asset/entry fields are pre-filled with the `getContent` method so I assume this is just a Craft bug...  
  
  ---
  
  ### Answer by username_4 on 2020-10-30. Score: 3
  
  **Craft 3**
  
  ```php
  $entry->setFieldValue('my_field_handle', 'new_value');
  $app->elements->saveElement($entry);
  
  ```
  
  
  ---
  
  ### Answer by username_5 on 2016-01-14. Score: 3
  
  I'm not completely sure of the technical reasons (I guess something to do with relation fields not have a column in the `craft_content` table), but you also need to re-save the existing ids for any relation fields (Assets, Entries, Categories, Matrix, etc) before the element can be saved:
  
  ```
  $entry->setContentFromPost(array(
      'metaStatus'  => 'Declined',
      'assetsField' => $entry->assetsField->ids(),
      'matrixField' => $entry->matrixField->ids(),
  );
  
  craft()->entries->saveEntry($entry);
  
  ```
  
  
  ---
  
  