<p>Your code looks right. I'd double check for spelling mistakes and check if there's a entry model returned from your <code>$event = $criteria-&gt;first();</code> by outputting its ID with <code>return $event-&gt;id</code>.</p>

CC BY-SA 3.0
2016-01-28T14:10:48.327Z
yes, there is the id that I can output. Also I can output the `$event->title`. Very weird behaviour detected: In my code above I changed `$criteria->section = 'offlineEvents';` to `$criteria->section = 'products';` The products have their own matrix field `productDetailBody`. And now I can do `foreach ($event->productDetailBody as $details) { ... }` This in fact works. I double checked the Events entry created correctly, I even deleted it and created one from scratch, trying spelling different handlers. Also new events entries give 404 on frontend. any thoughts?
Almazik G
CC BY-SA 3.0
2016-01-28T14:20:04.310Z
Maybe your criteria model isnt explicit enough and you're getting an entry back with an empty matrix field. Try to be more restricive, by adding an `$criteria->entryType = 'offlineEvents';` or for testing purposes an `$criteria->id = 24;` filter.
carlcs
CC BY-SA 3.0
2016-01-28T14:30:13.730Z
nope, doesn't work either. I think I'm missing something. I just created simples `Test` section, type of `Channel`, added a test entry into it, named test 1, and I navigate on frontend to /test/test-1 and it's 404. Also there is no preview button on admin while editing the entry. In the Section edit screen there is the checkbox 'each entry has its own url'... I recentlry upgraded craft to latest version, I thing this is somehow related..
Almazik G
CC BY-SA 3.0
2016-01-28T14:33:34.343Z
This /test/test-1 routing issue is unrelated to getting an element model via a method like `craft()->elements->getCriteria('Entry');`.
carlcs
CC BY-SA 3.0
2016-01-28T14:35:51.257Z
Agreed, it's not related. Do you have something in mind as to 404 issue?
Almazik G
CC BY-SA 3.0
2016-01-28T11:54:57.700Z
2016-01-28T11:54:57.700Z
carlcs
<p>In my controller I fetched the <code>Entry</code></p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria('Entry');
$criteria-&gt;section = 'offlineEvents';
$event = $criteria-&gt;first();
</code></pre>

<p>The Entry has a matrix field with handler called <code>eventDetails</code></p>

<p>How can I access this matrix field values? I want to do it like I'd do it in template:</p>

<pre><code>{% for items in entry.eventDetails %}
    {% if items.getType() == 'heading' %}  
     ....
</code></pre>

<p>but I cant do </p>

<pre><code>foreach ($event-&gt;eventDetails as $details) { ... }
</code></pre>

<p>it's giving me empty array.</p>

<matrix><controller>
Fetch matrix field attributes in controller
# Fetch matrix field attributes in controller

(Asked by: username_0 on 2016-01-28)

In my controller I fetched the Entry
$criteria = craft()->elements->getCriteria('Entry');
$criteria->section = 'offlineEvents';
$event = $criteria->first();

The Entry has a matrix field with handler called eventDetails
How can I access this matrix field values? I want to do it like I'd do it in template:
{% for items in entry.eventDetails %}
    {% if items.getType() == 'heading' %}  
     ....

but I cant do 
foreach ($event->eventDetails as $details) { ... }

it's giving me empty array.

**Tags:** matrix, controller


---

## 1 answers

### Answer by username_1 on 2016-01-28. Score: 1

Your code looks right. I'd double check for spelling mistakes and check if there's a entry model returned from your `$event = $criteria->first();` by outputting its ID with `return $event->id`.

> **Comments:**
> **username_0**: yes, there is the id that I can output. Also I can output the `$event->title`. Very weird behaviour detected: In my code above I changed `$criteria->section = 'offlineEvents';` to `$criteria->section = 'products';` The products have their own matrix field `productDetailBody`. And now I can do `foreach ($event->productDetailBody as $details) { ... }` This in fact works. I double checked the Events entry created correctly, I even deleted it and created one from scratch, trying spelling different handlers. Also new events entries give 404 on frontend. any thoughts?  
> **username_1**: Maybe your criteria model isnt explicit enough and you're getting an entry back with an empty matrix field. Try to be more restricive, by adding an `$criteria->entryType = 'offlineEvents';` or for testing purposes an `$criteria->id = 24;` filter.  
> **username_0**: nope, doesn't work either. I think I'm missing something. I just created simples `Test` section, type of `Channel`, added a test entry into it, named test 1, and I navigate on frontend to /test/test-1 and it's 404. Also there is no preview button on admin while editing the entry. In the Section edit screen there is the checkbox 'each entry has its own url'... I recentlry upgraded craft to latest version, I thing this is somehow related..  
> **username_1**: This /test/test-1 routing issue is unrelated to getting an element model via a method like `craft()->elements->getCriteria('Entry');`.  
> **username_0**: Agreed, it's not related. Do you have something in mind as to 404 issue?  

---

