php - One-to-one association in form? -
in symfony 2.0, how create drop down list using one-to-one association in form? can guys put example please?
i try answer question way understand it. let's have faculty
object bound single university
object. in form used create or edit faculty, display combo box of university in database , user choose 1 among them. there 1 special symfony field type this: entity type. below code of buildform
method use in facultytype
object used create faculty form:
// application\acmebundle\form\type\facultytype public function buildform(formbuilder $builder, array $options) { $builder->add('name'); $builder->add('university', 'entity', array( // class of entity used combo box item 'class' => 'acmebundle:university', // property of entity displaying entity text 'property' => 'name', // query builder used populate combo box, accepts // querybuilder object or \closure below 'query_builder' => function(entityrepository $repository) { // return query builder selecting universities return $repository->createquerybuilder('u'); } )); }
note: there other properties can set entity field type, invite take @ page more information on it.
rendered, show combo box universities have set in database. when user save form, university chose assigned faculty object bound form via setter. render drop-down list instead of combo box. if need select multiple entities, 'multiple'
option of field type entity useful.
this being said, example showed not one-to-one relation rather many-to-one faculty
object , one-to-many university
object. one-to-one relation more relation university
has unique address
. in case, combo box wouldn't useful since university can have 1 adress sub-form more appropriate. if has many adresses, becomes one-to-many relation relation between university , faculties.
not sure if answer question correctly hope lead final solution.
regards, matt
Comments
Post a Comment