Why does field value get updated on $element->getContent()->fieldHandle but not when accessed directly $element->fieldHandle? 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-&gt;element.  The handle for the field I want to be updating the value of is available via $this-&gt;model-&gt;handle.

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

$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;


Why am I able to successfully update the value for the getContent() method but not when I access it directly on the model?
setContentFromPost() performs an operation on the attached 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() 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 onAfterElementSave &mdash; your service can use getContent() to or getContentFromPost() 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.