简体   繁体   中英

How to display data with a ManyToMany relation and a joint table?

i'm currently working on a symfony project and i have a problem.

This project is a website about a theater. There is therefore an "actors" entity and a "shows" entity. Each show can have multiple comedians, and comedians can have multiple shows. So I generated a ManyToMany relationship, and got a "spectacles.actors" link table. I don't understand how I can display in my twig, for each actor, the shows in which he is present.

enter image description here

I tried

{{ actors.spectacles.title }} 

but its not working, i also tried a double for loop, but still not working. Can you help me please?

Thank you


Ok it doesn't work but I found why. In fact with my ManyToMany relationship, I got two binding tables: actors_spectacles and spectacles_actors. When I perform my fixtures, the spectacles_actors table is filled but not actor_spectacles.

But how to fill it? Since there is a certain order in fixtures, I am having a lot of trouble understanding how fixtures work with ManyToMany relationship, can you help me?

单击此处查看表格管理器

You should loop over each actor and each linked shows to the actor.

Let's say you fetched all the actors in your controller:

{% for actor in actors %}
<ul>
    {% for spectacle in actor.spectacles %}
        <li>{{ spectacle.title }}</li>
    {% endfor %}
</ul>
{% endfor %}

ps: I advise you to use singular form for naming your entities;)

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