Twig - 访问用户对象ID

时间:2016-04-26 16:58:10

标签: php twig symfony

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

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

我得到了结果:

User {#236 ▼
  -id: 1
  #email: "admin@admin.pl"
  -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 个答案:

答案 0 :(得分:2)

正如上面的评论中所讨论的,将公共getter函数getId添加到实体User有帮助:

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