Skip to content
Advertisement

PHP Symfony Many to Many form

I have a many-to-many relationship between two tables in my database (1 artikel has more than 1 bestelling and vice versa), so I made a link table(bestelregel).

But now I want to make a form so that I can add a new order with multiple products, but as I am new to this, I am not sure how to do it. I’ve tried to make a form for the entity Bestelling so that I can make a new order. And tried adding products to the order with another form(form based on the link table), but I recieved the following error:

Expected value of type “AppBundleEntityArtikel” for association field “AppBundleEntityBestelregel#$bestelordernummer”, got “AppBundleEntityBestelling” instead.

I hope you guys can help me. Maybe with another way?

My code:

Entity Artikel

JavaScript

Entity Bestelling

JavaScript

Entity Bestelregel (Link table)

JavaScript

Form Bestelling

JavaScript

Form Bestelregel (Link table)

JavaScript

Picture Database

Pic Database

Advertisement

Answer

You don’e need to add an intermediate entity (bestelregel here), unless it has to have its own unique properties (like the timestamp for which the relation stablished, or the user who stablished the relation or anything else).

In other words, if your bestelregel entity has ONLY this list of properties:

  • id
  • artikle
  • bestelling (or bestelorder or whatever you call it in Dutch)

forget about creating it. you only need these two entity classes:

  • Beselling
  • Artikel

Taking advantage of Doctrine ORM relations, the relation database table would be created and manipulated automatically behind the scene.

Also, keep your code in English, as you have the opportunity of a World get in touch in case you need a hand, instead of using Dutch or any other language. You may use translation tools to translate the interface, but use English for your code base as it is a best practice all over the world.

Article entity:

JavaScript

Order entity:

JavaScript

OrderType form:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement