<p>I had the same problem a couple of weeks ago: apparently there isn't a simple function that can be called as getAttributes only returns data from the content table.</p>

<p>I wrote the code below to get data for all matrix fields within an entry, but later on I realised that there are other fields which appear null from getAttributes such as asset and entry fields.</p>

<p>Anyone else have a rock-solid method for all data?</p>

<pre><code>$content = $entry-&gt;getContent();

$data = $content-&gt;getAttributes(null,true);

    // Getting content / attributes doesn't appear to work for Matrix fields? - get content and re-populate data array
    $matrixFields = craft()-&gt;db-&gt;createCommand()-&gt;select(array('handle'))-&gt;where( array('type' =&gt; 'Matrix') )-&gt;from('fields')-&gt;queryAll();

    foreach( $matrixFields as $matrix )
    {
        $handle = $matrix['handle'];

        if( $entry-&gt;$handle )
        {
            $attributes = array();
            foreach( $entry-&gt;$handle as $matrix )
            {
                $attr =  $matrix-&gt;getContent()-&gt;getAttributes();

                // Attribute Formatting (this is formatting for my use-case - feel free to amend as you need)
                unset( $attr['id'] , $attr['elementId'] , $attr['locale'] , $attr['title'] );
                $attr = array_merge( $attr , array("___type" =&gt; $matrix-&gt;type-&gt;handle ) );

                $attributes[] = $attr;
            }
            $data[$handle] = $attributes;
        }
    }
</code></pre>

CC BY-SA 3.0
2014-10-24T22:06:19.227Z
2014-10-24T22:06:19.227Z
George-Reactor
<p>Reference: <a href="https://craftcms.stackexchange.com/questions/4486/what-is-getcontent-the-contentmodel-and-how-do-i-use-it-to-get-and-set-cust/4487?noredirect=1#comment5139_4487">What is getContent() / the ContentModel, and how do I use it to get and set custom field values?</a></p>

<p>In my case, I wanted to get all attributes, and all content attributes. Here's my approach: <a href="http://gist.github.com/timkelty/2a221ac232e5ecf1b090" rel="nofollow noreferrer">http://gist.github.com/timkelty/2a221ac232e5ecf1b090</a></p>

<p>Curious to see if there are alternate solutions, though.</p>

CC BY-SA 3.0
2014-11-04T02:57:48.223Z
2015-04-16T12:59:07.057Z
2017-04-13T12:51:00.160Z
Tim Kelty
<p>ContentModel will only have the values in the actual content table, and the values that are actually there still may not be what you’re after, because accessing those values directly won’t give the field types a chance to do any preprocessing on their data.</p>
<p>So the best way to reliably get an array of all of an element’s custom field content is to go through its Field Layout, via <a href="http://buildwithcraft.com/classreference/models/BaseElementModel#getFieldLayout-detail" rel="nofollow noreferrer">BaseElementModel::getFieldLayout()</a>:</p>
<pre class="lang-php prettyprint-override"><code>// Craft 2
$values = array();

foreach ($element-&gt;getFieldLayout()-&gt;getFields() as $fieldLayoutField)
{
    // $fieldLayoutField is a FieldLayoutFieldModel, *not* a FieldModel
    // http://buildwithcraft.com/classreference/models/FieldLayoutFieldModel
    // so get the actual FieldModel here
    $field = $fieldLayoutField-&gt;getField();

    // Now get the prepped field value
    $value = $element-&gt;getFieldValue($field-&gt;handle);

    $values[$field-&gt;handle] = $value;
}
</code></pre>
<p><strong>Update:</strong> As of Craft 3, you can get all the custom field values using <code>getFieldValues()</code>:</p>
<pre class="lang-php prettyprint-override"><code>$values = $element-&gt;getFieldValues();
</code></pre>

CC BY-SA 3.0
2014-12-02T18:40:40.713Z
Is there a twig equivalent of this and will it get everything from an entry whatever it contains?
Spoeken
CC BY-SA 3.0
2014-12-03T13:44:52.940Z
@Spoeken Same methods; different syntax: http://craftcms.stackexchange.com/questions/4753/how-to-get-a-list-of-fields-on-an-entry/4755#4755
Brandon Kelly
CC BY-SA 4.0
2022-05-23T20:21:17.553Z
@BrandonKelly Is this answer correct for Craft 3 and Craft 4?  Or is there a different way to do this now?  I'm having the same problem here in 2022
vlasits
CC BY-SA 4.0
2022-05-24T20:56:01.263Z
@Spoeken This was for Craft 2. Updated the answer for Craft 3+.
Brandon Kelly
CC BY-SA 4.0
2014-11-04T17:34:49.007Z
2022-05-24T20:55:36.050Z
2022-05-24T20:55:36.050Z
Brandon Kelly
<p>Initially I was doing <code>$entry-&gt;getContent()-&gt;getAttributes()</code>, but noticed my Matrix fields were all <code>null</code>.</p>

<p>What is the right way to do this?</p>

<p>I see that <code>$entry-&gt;myMatrixField</code> gives me the <code>ElementCriteriaModel</code> for my Matrix that I need, I thought that would be the same as using `getAttribute('myMatrixField'), but i guess not.</p>

<plugin-development><matrix><entrymodel>
Getting all content, including Matrix fields from an EntryModel
# Getting all content, including Matrix fields from an EntryModel

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

Initially I was doing `$entry->getContent()->getAttributes()`, but noticed my Matrix fields were all `null`.

What is the right way to do this?

I see that `$entry->myMatrixField` gives me the `ElementCriteriaModel` for my Matrix that I need, I thought that would be the same as using `getAttribute('myMatrixField'), but i guess not.

**Tags:** plugin-development, matrix, entrymodel

---

## 3 answers

### Answer by username_2 on 2014-11-04. Score: 5 | Accepted answer

ContentModel will only have the values in the actual content table, and the values that are actually there still may not be what you’re after, because accessing those values directly won’t give the field types a chance to do any preprocessing on their data.
So the best way to reliably get an array of all of an element’s custom field content is to go through its Field Layout, via BaseElementModel::getFieldLayout():
// Craft 2
$values = array();

foreach ($element->getFieldLayout()->getFields() as $fieldLayoutField)
{
    // $fieldLayoutField is a FieldLayoutFieldModel, *not* a FieldModel
    // http://buildwithcraft.com/classreference/models/FieldLayoutFieldModel
    // so get the actual FieldModel here
    $field = $fieldLayoutField->getField();

    // Now get the prepped field value
    $value = $element->getFieldValue($field->handle);

    $values[$field->handle] = $value;
}

Update: As of Craft 3, you can get all the custom field values using getFieldValues():
$values = $element->getFieldValues();

> **Comments:**
> **username_3**: Is there a twig equivalent of this and will it get everything from an entry whatever it contains?  
> **username_2**: @Spoeken Same methods; different syntax: http://craftcms.stackexchange.com/questions/4753/how-to-get-a-list-of-fields-on-an-entry/4755#4755  
> **username_4**: @BrandonKelly Is this answer correct for Craft 3 and Craft 4?  Or is there a different way to do this now?  I'm having the same problem here in 2022  
> **username_2**: @Spoeken This was for Craft 2. Updated the answer for Craft 3+.  

---

### Answer by username_1 on 2014-10-24. Score: 2

I had the same problem a couple of weeks ago: apparently there isn't a simple function that can be called as getAttributes only returns data from the content table.
I wrote the code below to get data for all matrix fields within an entry, but later on I realised that there are other fields which appear null from getAttributes such as asset and entry fields.
Anyone else have a rock-solid method for all data?
$content = $entry->getContent();

$data = $content->getAttributes(null,true);

    // Getting content / attributes doesn't appear to work for Matrix fields? - get content and re-populate data array
    $matrixFields = craft()->db->createCommand()->select(array('handle'))->where( array('type' => 'Matrix') )->from('fields')->queryAll();

    foreach( $matrixFields as $matrix )
    {
        $handle = $matrix['handle'];

        if( $entry->$handle )
        {
            $attributes = array();
            foreach( $entry->$handle as $matrix )
            {
                $attr =  $matrix->getContent()->getAttributes();

                // Attribute Formatting (this is formatting for my use-case - feel free to amend as you need)
                unset( $attr['id'] , $attr['elementId'] , $attr['locale'] , $attr['title'] );
                $attr = array_merge( $attr , array("___type" => $matrix->type->handle ) );

                $attributes[] = $attr;
            }
            $data[$handle] = $attributes;
        }
    }


---

### Answer by username_0 on 2014-11-04. Score: 1

Reference: [What is getContent() / the ContentModel, and how do I use it to get and set custom field values?](https://craftcms.stackexchange.com/questions/4486/what-is-getcontent-the-contentmodel-and-how-do-i-use-it-to-get-and-set-cust/4487?noredirect=1#comment5139_4487)

In my case, I wanted to get all attributes, and all content attributes. Here's my approach: <http://gist.github.com/timkelty/2a221ac232e5ecf1b090>

Curious to see if there are alternate solutions, though.


---

