kris_IV kris_IV - 2 years ago 163
PHP Question

Twig - access to user object id

I have problem with display id for user object in Twig (Symfony 3). For example I can dump User object:

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

and I get result:

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

I can display all elements for this object:

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

But I can't display ID:

<td>{{ }}</td>

When I tray to use code above I get error message:

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

Answer Source

As discussed in comments above, adding public getter function getId to entity User helped:

public function getId() {
    return $this->id;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download