How to create a form with multiple rows of one entity in Symfony2

前端 未结 2 2085
情话喂你
情话喂你 2020-12-11 03:39

First I\'ve read documents for both Collection Field Type and How to Embed a Collection of Forms ... The example is about one entity (Task) that has one-to-many relation wit

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-11 04:18

    Here's a start on a possible solution. The example below takes a single entity Skills and presents all of them on a single page. What I don't know is whether this technique can be used to persist children objects. I would expect one could loop through the returned data and persist as required.

    The code below results in a page with a list of all possible Skills and a checkbox for declaring each enabled or enabled.

    In a controller:

        $skills = $em->getRepository("TruckeeMatchingBundle:Skill")->getSkills();
        $formSkills = $this->createForm(new SkillsType(), array('skills' => $skills));
        ...
            if ($request->getMethod() == 'POST') {
                $formSkills->handleRequest($request);
                foreach ($skills as $existingSkill) {
                    $em->persist($existingSkill);
                }
            }
        ...
        return ['formSkills' => $formSkills->createView(),...]
    

    In a template:

    {% for skill in formSkills.skills %}
        {{ skill.vars.value.skill }}
                
                

提交回复
热议问题