Answers.Body: 
  
  <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-31T20:06:43.030Z
Answers.Text: I suspect this is related to my question :)
Answers.UserDisplayName: Tyler Willingham
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-31T20:10:47.557Z
Answers.Text: Doesn't that make [this example](http://buildwithcraft.com/classreference/services/EntriesService#saveEntry-detail) inaccurate?
Answers.UserDisplayName: Tyler Willingham
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-31T20:20:17.123Z
Answers.Text: @TylerWillingham Yep, just updated the DocBlock for the next release, sorry about that.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-31T20:23:44.157Z
Answers.Text: 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 :)
Answers.UserDisplayName: Tyler Willingham
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-03T22:00:20.097Z
Answers.Text: 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
Answers.UserDisplayName: Tim Kelty
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-03T22:38:56.390Z
Answers.Text: @TimKelty Probably worth a new question ;)
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-04T02:55:08.483Z
Answers.Text: 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
Answers.UserDisplayName: Tim Kelty
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-20T20:25:09.477Z
Answers.Text: 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?
Answers.UserDisplayName: jthomas
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-15T17:15:01.537Z
Answers.Text: 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).
Answers.UserDisplayName: Benjamin Smith
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-31T20:03:17.287Z
Answers.LastActivityDate: 2016-10-20T17:28:51.697Z
Answers.LastEditDate: 2016-10-20T17:28:51.697Z
Answers.UserDisplayName: Brandon Kelly
Body: <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>
  
Tags: <plugin-development><elementtype><entrymodel><contentmodel><baseelementmodel>
Title: What is getContent() / the ContentModel, and how do I use it to get and set custom field values?
ThreadText: # 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()](http://buildwithcraft.com/classreference/models/BaseElementModel#getContent-detail) will return the element’s [ContentModel](http://buildwithcraft.com/classreference/models/ContentModel). It has two purposes:
  
  1. To store the element’s data from the craft\_content table table when fetching elements from the DB.
  2. 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)`](http://buildwithcraft.com/classreference/models/BaseElementModel#getFieldValue-detail), or simply type `$entry->$fieldHandle`, since BaseElementModel’s [magic getter](http://buildwithcraft.com/classreference/models/BaseElementModel#__get-detail) 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()](http://buildwithcraft.com/classreference/fieldtypes/IFieldType#prepValue-detail) 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)`](http://buildwithcraft.com/classreference/models/BaseElementModel#setContentFromPost-detail).
  
    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()](http://buildwithcraft.com/classreference/fieldtypes/IFieldType#prepValueFromPost-detail) 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.
  
    ```php
    $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**: @username_1 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/username_2/2a221ac232e5ecf1b090  
  > **username_0**: @username_2 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).  
  
  ---
  
  