简体   繁体   中英

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 %}

完全转储

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 %}

标签转储

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

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 %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM