简体   繁体   English

Twig没有在对象中显示数据

[英]Twig doesn't show data in object

I'm trying to use Twig to display a simple list of Doctrine 2 entity objects. 我正在尝试使用Twig显示Doctrine 2实体对象的简单列表。 The objects are fetched with the untouched results from a DQL query. 使用DQL查询的原始结果获取对象。 The PHP code is like this: PHP代码如下:

/*
 * I know the query in the Repository class works as I get the correct objects
 * from the method and can iterate through them with foreach in PHP, displaying
 * data from the object through getters, like ->getId()
 */
$received = $repo->getReceived($id);

/*
 * This appears to work, the variables are assigned correctly.
 */
echo $template->render(array("received" => $received, "first" => $received[0]));

The Twig template contains this: Twig模板包含以下内容:

{% for item in received %}
    <li>Item: {{ item.id }}, {{ first.id }}</li>
{% endfor %}

The strange thing is that twig iterates through the received array correctly, there are three objects in the array and there are three lines output but the id (or any other member) doesn't come out when accessed as item.id but the second output, first.id, comes out correctly. 奇怪的是,细枝正确地循环访问了接收到的数组,数组中有三个对象,并且输出了三行,但是当作为item.id访问时,id(或任何其他成员)没有出现,但是第二个输出,first.id,正确显示。

What I should be seeing is this: 我应该看到的是:

Item 1, 1
Item 2, 1
Item 3, 1

but instead I'm seeing: 但是我看到的是:

Item , 1
Item , 1
Item , 1

I've tried different variations to get the output but nothing has worked so far, item.getId, item.getId(). 我尝试了各种变体来获取输出,但是到目前为止,item.getId,item.getId()仍然没有任何作用。

Any ideas what I am missing here? 有什么想法我在这里想念的吗?

Edit dump output 编辑转储输出

{{ dump(received) }} returns the following: {{dump(received)}}返回以下内容:

array(3) {
  [0]=>
  object(Received)#219 (6) {
    ["id":"Received":private]=>
    int(1)
  ... snip ...
  }
  [1]=>
  object(Received)#208 (6) {
    ["id":"Received":private]=>
    int(2)
  ... snip ...
  }
  [2]=>
  object(Received)#210 (6) {
    ["id":"Received":private]=>
    int(3)
  ... snip ...

I've cut it down, these are three full blown Doctrine entity objects so they are in the array and look correct. 我已将其缩减,这是三个完整的Doctrine实体对象,因此它们位于数组中,并且看起来正确。

I've finally found a way around this although I can't understand why I need to do it. 尽管我不明白为什么需要这样做,但我终于找到了解决方法。 I changed the array to contain a set of arrays instead of objects but that didn't work either. 我将数组更改为包含一组数组,而不是对象,但这也不起作用。

I then created an alternative array for testing with the exact same structure and that array worked without any problems. 然后,我创建了另一个数组,用于测试具有完全相同的结构,并且该数组正常工作。 Anyone have any idea what might cause one array to work while the other fails? 任何人都不知道是什么导致一个阵列工作而另一个阵列出现故障? Both are two dimensional arrays. 两者都是二维数组。

So what I ended up with was that I used the two dimensional array and changed the twig code to: 所以我最终得到的是我使用了二维数组并将树枝代码更改为:

{% for item in received %}
    <li>Item: {{ attribute(item, 'id') }}</li>
{% endfor %}

For some reason attribute() is needed there although I did try that previously but then Twig complained that attribute() didn't exist. 由于某种原因,尽管我以前曾尝试过该属性,但仍需要那里的attribute(),但是Twig抱怨说attribute()不存在。

Hopefully this will help some others. 希望这会对其他人有所帮助。

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

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