Php 细枝不';不在对象中显示数据
我试图使用Twig来显示Doctrine 2实体对象的简单列表。从DQL查询中使用未触及的结果获取对象。 PHP代码如下所示:Php 细枝不';不在对象中显示数据,php,doctrine,twig,Php,Doctrine,Twig,我试图使用Twig来显示Doctrine 2实体对象的简单列表。从DQL查询中使用未触及的结果获取对象。 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
/*
* 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]));
细枝模板包含以下内容:
{% for item in received %}
<li>Item: {{ item.id }}, {{ first.id }}</li>
{% endfor %}
但我看到的是:
Item , 1
Item , 1
Item , 1
我尝试了不同的变体来获得输出,但到目前为止没有任何效果,item.getId,item.getId()
你知道我错过了什么吗
编辑转储输出
{{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 ...
我把它删减了,这是三个完整的条令实体对象,所以它们在数组中,看起来是正确的。我终于找到了解决这个问题的方法,尽管我不明白为什么我需要这样做。我将数组更改为包含一组数组而不是对象,但这也不起作用 然后我用完全相同的结构创建了一个用于测试的替代阵列,该阵列工作起来没有任何问题。有人知道是什么导致一个阵列工作而另一个阵列失败吗?两者都是二维数组 因此,我最终使用了二维数组,并将细枝代码更改为:
{% for item in received %}
<li>Item: {{ attribute(item, 'id') }}</li>
{% endfor %}
{已接收%%中的项目的%
项:{{属性(项,'id')}
{%endfor%}
出于某种原因,这里需要attribute(),虽然我以前确实尝试过,但后来Twig抱怨attribute()不存在
希望这会对其他人有所帮助。当您
转储(接收)
时会得到什么?我在问题中添加了一个缩写的转储输出。我感觉我缺少一些简单的配置或类似的东西。您有getId()
函数吗?您的成员定义为私有,因此twig无法直接访问它们,但将尝试调用getter。请阅读代码!是的,我有一个getId()函数,正如我在代码示例的第一个注释部分中提到的,它的工作方式与我在代码示例中所展示的一样。如果我将对象数组中的第一项单独传递给Twig,它将毫无问题地显示任何参数。
{% for item in received %}
<li>Item: {{ attribute(item, 'id') }}</li>
{% endfor %}