-1

I'm trying to make a table with all entities from the database. This works fine, but in the entity is only the user id saved.

How I can fetch the username?

{% for invite in invites %}
    <tr>
        <td>{{ invite.code}}</td>
        <td>{{ invite.creator }}</td> {# This line returns the user id, but how I can get the username? #}
        <td>{{ invite.creationdate|format_datetime(locale='de') }}</td>
        <td><a class="btn btn-danger btn-sm" href="{{ path('admin.invite.delete', {'id': invite.id}) }}">
                <i class="fas fa-trash">
                </i>
                Delete
            </a>
        </td>
    </tr>
{% endfor %}
Tutorialwork
  • 63
  • 2
  • 9

1 Answers1

0

If invite.creator is object, then you can access to its properties.

{{ invite.creator.username }} {# name property of User::class #}

If it is id only, then you need to pass array 'userIdToUser' into twig from your contoller

{{ userIdToUser[invite.creator] }}
ffx14
  • 1,825
  • 2
  • 14
  • 19
  • Thanks for your help, I got it. But is it a good idea to fetch every time when my table is called all users from the database in an array with the usernames? – Tutorialwork Apr 16 '20 at 10:47
  • Somehow you need to select names from database. You can select only id and name from users, and use something like userIdToName. This is the fastest way. Can you show code of your entities? – ffx14 Apr 16 '20 at 10:52