2016-04-26 46 views
1

我在Twig(Symfony 3)中的用户对象的显示ID有问题。例如,我可以转储用户对象:树枝 - 访问用户对象ID

{% for user in usersObject %} 
    {{ dump(user) }} 
{% endfor %} 

,我得到的结果:

User {#236 ▼ 
    -id: 1 
    #email: "[email protected]" 
    -username: "admin" 
    -password: "$2y$13$TW.gB06kBOBtc04Fti176OQBzNRh79G9tDIqNEf098/ADHsQkbM4W" 
    -permissions: "ROLE_SUPER_ADMIN" 
    -isActive: true 
    -createDate: DateTime {#232 ▶} 
    -lastLogin: DateTime {#233 ▶} 
    -userCookie: "" 
} 

我可以显示该对象的所有元素:

<tr> 
    <td>{{ user.username }}</td> 
    <td>{{ user.permissions }}</td> 
    <td>{{ user.password }}</td> 
</tr> 

但我不能显示ID :

<tr> 
    <td>{{ user.id }}</td> 
</tr> 

当我托盘使用上面的代码我收到错误消息:

Method "id" for object "...\....\...\...\....html.twig" does not exist in @.../admin/form/usersList.html.twig at line 15 
+1

如果您尝试添加getter方法,属性ID会发生什么:'public function getId(){return $ this-> id; ''用户'实体? – Miro

+0

你的答案解决了我的问题 - 现在一切正常。 你能解释为什么吗?我认为所有东西都发送到我的用户对象中(我从转储中看到了我的数据!),我可以从user.XXX而不是实体获取所有内容。 –

+0

你没有发布实体,但我怀疑,你没有getter和该属性被设置为私人(或受保护),所以枝不能到达它。如果你有所有类的getter函数和相关的属性,总是最好的...可能小枝使用不同的机制来显示转储,但我将不得不研究小枝的源代码...;) – Miro

回答

2

正如上述评论的讨论,增加公共getter函数getId到实体User帮助:

public function getId() { 
    return $this->id; 
}