Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't access nested object properties with TWIG

Tl;DR Twig won't let me drill down into a nested object.

I have this collection of json_decoded objects that has a nested object within it. When trying to output a property of the nested object I get an error like:

Item "text" for "" does not exist

when I try a to dump out the nested object i can see it fine... but I can't access any of it's properties. Here's the dump of the "entire" parent object

Using this in my loop
{% for item in allFields %}

    {{ dump(item) }}

{% endfor %}

Full Dump

And here is the dump of the nested label object its self using {{dump(item.label)}} in my loop

Using this in my loop
{% for item in allFields %}

    {{ dump(item.label) }}

{% endfor %}

Label Dump

I'm attempting to get the text property (and others) of the label class by using a twig for loop like so:

{% for item in allFields %}

    {{ item.label.text }}

{% endfor %}

And it is here that I get the error

Item "text" for "" does not exist

like image 623
Drew Landgrave Avatar asked Jan 29 '13 20:01

Drew Landgrave


1 Answers

That is weird. One though: this happened to me on one occasion when my EntityManager was running out of memory due to very complex hydration query. I'm thinking that some part of data gets lots here and you get this error.

So, how many item do you have in this allFields list?

In order to troubleshoot this I suggest you do:

{% for item in allFields %}

    {{ item.label is null or item.label == "" ? "***EMPTY-LABEL***" : item.label.text }}

{% endfor %}
like image 63
Jovan Perovic Avatar answered Oct 17 '22 16:10

Jovan Perovic