

<p><a href="http://buildwithcraft.com/classreference/models/BaseElementModel#getContent-detail" rel="nofollow">BaseElementModel::getContent()</a> will return the element’s <a href="http://buildwithcraft.com/classreference/models/ContentModel" rel="nofollow">ContentModel</a>. It has two purposes:</p>

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

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

<ul>
<li><p>When you’re <strong>getting</strong> an element’s existing custom field values, use <a href="http://buildwithcraft.com/classreference/models/BaseElementModel#getFieldValue-detail" rel="nofollow"><code>$entry-&gt;getFieldValue($fieldHandle)</code></a>, or simply type <code>$entry-&gt;$fieldHandle</code>, since BaseElementModel’s <a href="http://buildwithcraft.com/classreference/models/BaseElementModel#__get-detail" rel="nofollow">magic getter</a> will act as a wrapper for getFieldValue() if the attribute you’re accessing happens to be a custom field handle.</p>

<p>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 <a href="http://buildwithcraft.com/classreference/fieldtypes/IFieldType#prepValue-detail" rel="nofollow">prepValue()</a> method). Most field types have <em>something</em> 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 <em>any</em> 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 <code>$entry-&gt;getContent()-&gt;$fieldHandle</code> you would just get <code>null</code> back, no matter how much data the field actually has.</p></li>
<li><p>When you’re <strong>setting</strong> new custom field values on an element, use <a href="http://buildwithcraft.com/classreference/models/BaseElementModel#setContentFromPost-detail" rel="nofollow"><code>$entry-&gt;setContentFromPost($newValues)</code></a>.</p>

<p>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 <a href="http://buildwithcraft.com/classreference/fieldtypes/IFieldType#prepValueFromPost-detail" rel="nofollow">prepValueFromPost()</a> 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.</p>

<pre class="lang-php prettyprint-override"><code>$element-&gt;setContentFromPost(array(
    'myEntriesField' =&gt; array(1, 2, 3),
));
</code></pre></li>
</ul>

CC BY-SA 3.0
2014-10-31T20:06:43.030Z
I suspect this is related to my question :)
Tyler Willingham
CC BY-SA 3.0
2014-10-31T20:10:47.557Z
Doesn't that make [this example](http://buildwithcraft.com/classreference/services/EntriesService#saveEntry-detail) inaccurate?
Tyler Willingham
CC BY-SA 3.0
2014-10-31T20:20:17.123Z
@TylerWillingham Yep, just updated the DocBlock for the next release, sorry about that.
Brandon Kelly
CC BY-SA 3.0
2014-10-31T20:23:44.157Z
No worries. That's what I used as an example. I thought it would be good to bring it up to prevent anyone else from seeing it in the future :)
Tyler Willingham
CC BY-SA 3.0
2014-11-03T22:00:20.097Z
So, if I wanted to get an array of content by fieldHandles (including non-content attributes), does this make sense? https://gist.github.com/timkelty/2a221ac232e5ecf1b090
Tim Kelty
CC BY-SA 3.0
2014-11-03T22:38:56.390Z
@TimKelty Probably worth a new question ;)
Brandon Kelly
CC BY-SA 3.0
2014-11-04T02:55:08.483Z
Ok, basically the same question as this: http://craftcms.stackexchange.com/questions/4348/getting-all-content-including-matrix-fields-from-an-entrymodel I'll add there
Tim Kelty
CC BY-SA 3.0
2016-09-20T20:25:09.477Z
How would you **get** the value of an entries fields that has one or more entries in it? Would that be with the same approach linked to in the previous comment?
jthomas
CC BY-SA 3.0
2016-11-15T17:15:01.537Z
Old post, but strolled in from Google. It looks like the magic getter method is *not* a straight wrapper for `$entry->getFieldValue()`. Both call the `prepValue` method, whereas the magic getter does read directly from the content model in all circumstances (such as you are accessing a model property before it is saved).
Benjamin Smith
CC BY-SA 3.0
2014-10-31T20:03:17.287Z
2016-10-20T17:28:51.697Z
2016-10-20T17:28:51.697Z
Brandon Kelly
<p>If I want to retrieve content on an element, should I be calling <code>$entry-&gt;$fieldHandle</code> or <code>$entry-&gt;getContent()-&gt;$fieldHandle</code>? What’s the difference?</p>

<p>Likewise, if I’m setting new content on an element, what’s the recommended way to set my new custom field values?</p>

<plugin-development><elementtype><entrymodel><contentmodel><baseelementmodel>
What is getContent() / the ContentModel, and how do I use it to get and set custom field values?
# What is getContent() / the ContentModel, and how do I use it to get and set custom field values?

(Asked by: username_0 on 2014-10-31)

If I want to retrieve content on an element, should I be calling `$entry->$fieldHandle` or `$entry->getContent()->$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?

**Tags:** plugin-development, elementtype, entrymodel, contentmodel, baseelementmodel


---

## 1 answers

### Answer by username_0 on 2014-10-31. Score: 18 | Accepted answer

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->getFieldValue($fieldHandle), or simply type $entry->$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->getContent()->$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->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->setContentFromPost(array(
    'myEntriesField' => array(1, 2, 3),
));

> **Comments:**
> **username_1**: I suspect this is related to my question :)  
> **username_1**: Doesn't that make [this example](http://buildwithcraft.com/classreference/services/EntriesService#saveEntry-detail) inaccurate?  
> **username_0**: @TylerWillingham Yep, just updated the DocBlock for the next release, sorry about that.  
> **username_1**: No worries. That's what I used as an example. I thought it would be good to bring it up to prevent anyone else from seeing it in the future :)  
> **username_2**: So, if I wanted to get an array of content by fieldHandles (including non-content attributes), does this make sense? https://gist.github.com/timkelty/2a221ac232e5ecf1b090  
> **username_0**: @TimKelty Probably worth a new question ;)  
> **username_2**: Ok, basically the same question as this: http://craftcms.stackexchange.com/questions/4348/getting-all-content-including-matrix-fields-from-an-entrymodel I'll add there  
> **username_3**: How would you **get** the value of an entries fields that has one or more entries in it? Would that be with the same approach linked to in the previous comment?  
> **username_4**: Old post, but strolled in from Google. It looks like the magic getter method is *not* a straight wrapper for `$entry->getFieldValue()`. Both call the `prepValue` method, whereas the magic getter does read directly from the content model in all circumstances (such as you are accessing a model property before it is saved).  

---

