What is getContent() / the ContentModel, and how do I use it to get and set custom field values? If I want to retrieve content on an element, should I be calling $entry-&gt;$fieldHandle or $entry-&gt;getContent()-&gt;$fieldHandle? What’s the difference?

Likewise, if I’m setting new content on an element, what’s the recommended way to set my new custom field values?
BaseElementModel::getContent() will return the element’s ContentModel. It has two purposes:


To store the element’s data from the craft_content table table when fetching elements from the DB.
To store the POST data that should be stored in the craft_content table when saving an element to the DB.


In both cases, the ContentModel should not actually be accessed directly when getting/setting custom field values.


When you’re getting an element’s existing custom field values, use $entry-&gt;getFieldValue($fieldHandle), or simply type $entry-&gt;$fieldHandle, since BaseElementModel’s magic getter will act as a wrapper for getFieldValue() if the attribute you’re accessing happens to be a custom field handle.

You should do this rather than go directly to the ContentModel because it gives the field types a chance to prep the raw DB data (via their prepValue() method). Most field types have something they need to do before their data will be ready to be interacted with, and in the case of Matrix/relational fields, who don’t store any data in the craft_content table, it’s literally the only way you’ll have a chance to know anything about the field’s value. For those fields, if you were to call $entry-&gt;getContent()-&gt;$fieldHandle you would just get null back, no matter how much data the field actually has.
When you’re setting new custom field values on an element, use $entry-&gt;setContentFromPost($newValues).

Like getFieldValue()’s relationship to retrieving DB data, this method will give field types a chance to prep the POST data before it gets saved to the database (via their prepValueFromPost() method). Since Matrix/relational fields don’t actually store data in the database, this is actually a moot point here and their post data will end up on the ContentModel exactly how it was passed in to setContentFromPost(), but still, there’s no harm in using that method, and better to get in the habit of using it.

$element-&gt;setContentFromPost(array(
    'myEntriesField' =&gt; array(1, 2, 3),
));