15

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?

18

BaseElementModel::getContent() will return the element’s 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), 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),
    ));
    
  • I suspect this is related to my question :) – Tyler Willingham Oct 31 '14 at 20:06
  • Doesn't that make this example inaccurate? – Tyler Willingham Oct 31 '14 at 20:10
  • 1
    @TylerWillingham Yep, just updated the DocBlock for the next release, sorry about that. – Brandon Kelly Oct 31 '14 at 20:20
  • 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 Oct 31 '14 at 20:23
  • So, if I wanted to get an array of content by fieldHandles (including non-content attributes), does this make sense? gist.github.com/timkelty/2a221ac232e5ecf1b090 – Tim Kelty Nov 3 '14 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.