0

I'm new to Symfony and I would like your help in deciding the best way to design a form and store into database using Doctrine. Here is my task:

I have a form created with the FormBuilder containing various types of input fields. I have created entity which connects my database to form and it works both ways, both to fetch data and to put data into database.

However, I want to add a Select multiple field to a form which would be populated from a table from the database. And when I select the choices from that select element, I would like to save them into a database in either csv of id's or some simple serialized object or array.

So far, what I have managed to do is to add that Select field as EntityType and have placed ORM Annotation in entity for that element: Type("object"), which populates the Select element flawlessly. From the $_REQUEST variable I can see that it returns the id(s) of selected options. But, what I get when I submit the form is a very large serialized object in the field which is supposed to store selected element.

I suppose that it is a normal behavior (to store the entire object (an entire row of data)), but is there some cleaner, leaner way of storing only the id's that I would be able to later on use to populate the same select field, also using doctrine (since I don't have a need for other data in that field except for their IDs)?

Thank you in advance.

David Šili
  • 117
  • 2
  • 12
  • 1
    You should probably post your entities / form class(es) so we can see what exactly you mean. There's no "cleaner" way to store associations than using a foreign key in either one of the entities table or two foreign keys in a so called join-table. It is not exactly clear if we're talking about a many-to-one or many-to-many association here. Populating the field (eg. in an edit-form) should happen automatically, that's one of the purposes of using a framework. – ccKep May 04 '17 at 13:29
  • 1
    Saving all those IDs as a comma-separated values in a single-field actually violates database normalization (1NF) due to not being atomic. It also makes consistency a nightmare since the DBMS can't handle it for you (consider you saving a string "1,2,3,4" of foreign keys in a row and in the other table the row with id #3 gets deleted - you'd have inconsistent data) – ccKep May 04 '17 at 13:34

0 Answers0