Symfony 2 Embedded forms using one to many db relationship

前端 未结 2 2114
鱼传尺愫
鱼传尺愫 2021-02-09 06:17

I\'m have a problem embedding forms from different entities in one form, my form is being displayed with firstname [input] lastname [input] address - but the address has no inpu

相关标签:
2条回答
  • 2021-02-09 07:15

    Llewellyn, do you mean something like thid:

    public function editAction($id) { $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('imBundle:Inspecciones')->find($id);
    
        $entity_valores = $em->getRepository('imBundle:ValoresInspecciones')->findByInspecciones($id);
    
        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Inspecciones entity.');
        }
    
        $entity->setValoresInspecciones($entity_valores);
    
    
    
        $editForm = $this->createEditForm($entity);
        $deleteForm = $this->createDeleteForm($id);
    
        return $this->render('imBundle:Inspecciones:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }
    
    0 讨论(0)
  • 2021-02-09 07:16

    Oh I faced the same problem, but I found the solution, hope this will help you :-)

    You're forgetting to add an Address object to the member entity.

    In your action you'll need to do the following:

    $member = new Member();
    $member->addAddress(new Address());
    
    $form = $this->createForm(new MemberType(), $member);
    

    And then in your template:

     {% for address in form.address %}
      {{ form_widget(address.firstLine) }}
     {% endfor %}
    

    Btw your 'firstline' widget doesn't relate to an entity property.

    Btw if you called addAddress two times, you would of course get two 'firstline' widgets in your form.

    Hope this works. best of luck.

    0 讨论(0)
提交回复
热议问题