简体   繁体   English

无法使用TWIG访问嵌套对象属性

[英]Can't access nested object properties with TWIG

Tl;DR Twig won't let me drill down into a nested object. Tl; DR Twig不会让我深入到嵌套对象中。

I have this collection of json_decoded objects that has a nested object within it. 我有这个json_decoded对象的集合,其中有一个嵌套对象。 When trying to output a property of the nested object I get an error like: 当尝试输出嵌套对象的属性时,我得到一个错误,如:

Item "text" for "" does not exist “”的项目“text”不存在

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 这里是嵌套标签对象的转储,它在我的循环中使用{{dump(item.label)}}

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: 我试图通过使用twig for循环获取标签类的text属性(和其他),如下所示:

{% for item in allFields %}

    {{ item.label.text }}

{% endfor %}

And it is here that I get the error 而且我在这里得到了错误

Item "text" for "" does not exist “”的项目“text”不存在

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. 一个问题:有一次,当我的EntityManager由于非常复杂的水合查询而内存不足时,这种情况发生在我身上。 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? 那么,你在这个allFields列表中有多少项?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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