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?

1 Answer 1

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),
    ));
    
9
  • I suspect this is related to my question :) Oct 31, 2014 at 20:06
  • Doesn't that make this example inaccurate? Oct 31, 2014 at 20:10
  • 1
    @TylerWillingham Yep, just updated the DocBlock for the next release, sorry about that. Oct 31, 2014 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 :) Oct 31, 2014 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, 2014 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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