Search code examples
phpformssymfonymany-to-manyone-to-many

Symfony 2.8 : one to many relationship


I have two tables, Event and Product, one Event has multiple products and one product can be in multiple Events. Here I think it's a OneToMany Unidirectional relationship. In the form of Event creation I want to have the list of available products in the database and add some products to the event (collection of products).

What's the best way to do that?


  1. Implement the onetomany or
  2. Go in the way to implement ManyToMany and create a third table EventProducts

Can anyone help me to choose the best way and how to implement it?


Solution

  • 1)Implement the onetomany:

    In this case you can get the event products list : $event->getProducts

    But when you need to access to the event from the product entity via $product->getEvent() !!!!!! this is impossible because the relation is unidirectional

    2) Go in the way to implement ManyToMany and create a third table EventProducts :

    The solution is this one and create the third entity EventProduct

    Hope this can helps