<p><a href="https://craftcms.com/classreference/models/ContentModel"><code>setContentFromPost()</code></a> performs an operation on the attached <em><a href="https://craftcms.com/classreference/models/ContentModel">ContentModel</a></em>, not on the element model itself.</p>

<p>When you initialize a <em>BaseElementModel</em>, its content attributes are set <em>from</em> the associated <em>ContentModel</em>. ("Content attributes" means attributes from custom field data, a.k.a. the stuff you'll typically find in the <code>craft_content</code> DB table for a given element instance.)</p>

<p>However, later updating the properties of the content model does not immediately ripple into changing the attributes of the element model: You'll need to re-save the element after you do a <code>setContentFromPost()</code>, and <em>then</em> its magic attributes will reflect your newly set values.</p>

<p>When you <code>setContentFromPost()</code>, you're changing the data that will ultimately gets passed into each FieldType's <a href="https://craftcms.com/classreference/models/BaseElementModel#getContentFromPost-detail"><code>prepValueFromPost()</code></a> method. If you need to do additional processing later (after each FieldType has done its thing) &mdash; such as saving records in an third-party table during <code>onAfterElementSave</code> &mdash; your service can use <code>getContent()</code> to or <a href="https://craftcms.com/classreference/models/BaseElementModel#getContentFromPost-detail"><code>getContentFromPost()</code></a> to grab the values it needs, depending on whether you want to access the raw POST data the user submitted, or the values that got returned from each FieldType's <code>prepValue()</code> method.</p>

CC BY-SA 3.0
2016-02-14T19:41:05.717Z
2016-02-14T19:41:05.717Z
Michael Rog
<p>I am trying to update a custom field on an EntryModel.  I'm having half success, and I don't understand why. I've looked at other posts <a href="https://craftcms.stackexchange.com/a/4487/115">such as this one by Brandon</a> which discuss how to assign these values and I'm still not having any luck.</p>

<p>The EntryModel I'm trying to update is available in my field type in the <code>onAfterElementSave</code> method via <code>$this-&gt;element</code>.  The handle for the field I want to be updating the value of is available via <code>$this-&gt;model-&gt;handle</code>.</p>

<p>I have tried using <code>setContentFromPost()</code>, <code>setContent()</code>, <code>getContent()-&gt;setAttributes()</code> and they all seem to return the same results.</p>

<pre><code>$fieldHandle = $this-&gt;model-&gt;handle;

$this-&gt;element-&gt;setContentFromPost(array(
    $fieldHandle =&gt; 'changedValue'
));

// Accessing the value via getContent returns the update value 'changedValue'
$changedValue = $this-&gt;element-&gt;getContent()-&gt;fieldHandle;

// Accessing the value directly returns the 'originalValue'
$originalValue = $this-&gt;element-&gt;fieldHandle;
</code></pre>

<p>Why am I able to successfully update the value for the <code>getContent()</code> method but not when I access it directly on the model?</p>

<custom-fieldtype><entrymodel><contentmodel>
Why does field value get updated on $element->getContent()->fieldHandle but not when accessed directly $element->fieldHandle?
# Why does field value get updated on $element->getContent()->fieldHandle but not when accessed directly $element->fieldHandle?

(Asked by: username_0 on 2016-02-01)

I am trying to update a custom field on an EntryModel.  I'm having half success, and I don't understand why. I've looked at other posts such as this one by Brandon which discuss how to assign these values and I'm still not having any luck.
The EntryModel I'm trying to update is available in my field type in the onAfterElementSave method via $this->element.  The handle for the field I want to be updating the value of is available via $this->model->handle.
I have tried using setContentFromPost(), setContent(), getContent()->setAttributes() and they all seem to return the same results.
$fieldHandle = $this->model->handle;

$this->element->setContentFromPost(array(
    $fieldHandle => 'changedValue'
));

// Accessing the value via getContent returns the update value 'changedValue'
$changedValue = $this->element->getContent()->fieldHandle;

// Accessing the value directly returns the 'originalValue'
$originalValue = $this->element->fieldHandle;

Why am I able to successfully update the value for the getContent() method but not when I access it directly on the model?

**Tags:** custom-fieldtype, entrymodel, contentmodel


---

## 1 answers

### Answer by username_1 on 2016-02-14. Score: 4 | Accepted answer

[`setContentFromPost()`](https://craftcms.com/classreference/models/ContentModel) performs an operation on the attached *[ContentModel](https://craftcms.com/classreference/models/ContentModel)*, not on the element model itself.

When you initialize a *BaseElementModel*, its content attributes are set *from* the associated *ContentModel*. ("Content attributes" means attributes from custom field data, a.k.a. the stuff you'll typically find in the `craft_content` DB table for a given element instance.)

However, later updating the properties of the content model does not immediately ripple into changing the attributes of the element model: You'll need to re-save the element after you do a `setContentFromPost()`, and *then* its magic attributes will reflect your newly set values.

When you `setContentFromPost()`, you're changing the data that will ultimately gets passed into each FieldType's [`prepValueFromPost()`](https://craftcms.com/classreference/models/BaseElementModel#getContentFromPost-detail) method. If you need to do additional processing later (after each FieldType has done its thing) — such as saving records in an third-party table during `onAfterElementSave` — your service can use `getContent()` to or [`getContentFromPost()`](https://craftcms.com/classreference/models/BaseElementModel#getContentFromPost-detail) to grab the values it needs, depending on whether you want to access the raw POST data the user submitted, or the values that got returned from each FieldType's `prepValue()` method.


---

