In symfony 2.0, how to create a drop down list using one-to-one association in form? Can you guys put good example please?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I will try to answer your question the way I understand it. Let’s say I have a
Facultyobject bound to a singleUniversityobject. So in the form used to create or edit a faculty, I display a combo box of all the university in the database and the user choose one among them. There is one special Symfony field type that does exactly this: the entity type. Below is the code of thebuildFormmethod that I use in myFacultyTypeobject used to create the faculty form:Note: There are other properties that can be set for the entity field type, I invite you to take a look at this page for more information on it.
Rendered, this will show a combo box with all the universities I have set in the database. When the user save the form, the university chose is assigned to the faculty object bound to the form via a setter. You could probably render a drop-down list instead of a combo box. If you need to select multiple entities, the
'multiple'option of the field type entity could be useful.This being said, the example I showed is not a One-to-One relation but rather a Many-to-One for the
Facultyobject and a One-to-Many for theUniversityobject. A One-to-One relation would be something more like a relation where aUniversityhas a uniqueAddress. In this case, a combo box wouldn’t be useful since the university can only have one adress so a sub-form would be more appropriate. If it has many adresses, then it becomes a One-to-Many relation like the relation between the university and its faculties.Not sure if this will answer your question correctly but I hope it will lead you to a final solution.
Regards,
Matt