简体   繁体   中英

symfony2 get entity by id on other bundle template

I already have a complete web application that was developed for almost two years, but due to some inexperience in the beginning, it was developed in flat PHP. Now I'm trying to replicate the same application but using symfony2 and came across some difficulties.

One of it is that I have several lists of items that are retrieved from the database and by using the ID I can edit those items. What happens is that for listing the item I already have some information that are displayed in the list. For instance the name of the user that has created the item. Of course the name of the user is not stored in that particular table.. instead it is its ID and using a simple function ( getUserFullname($id) ) I get the user name e display it in the list.

Now on symfony2 I've created a controller that creates a repository and sends it to a twig template with a for cycle that lists all the items.. but the id of the user is a number.. I also have created an "User" entity to retrieve the information.

How can I get a specific user information by its "id" and display on each item list?

You need to define a relationship between the items and users. Then you'll be able to access the user's properties somewhat like this:

{{ item.user.fullName }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM