Symfony Insert collection of multiple form tables

I have 3 tables / entities. I am trying to create a form to save data in all three tables in one form. The entities are longer, so I have not added here.

Client Entity I did an array assembly in the client entity for the other two entities.

    protected $Clientservice;
    protected $Hostingaccount;

    public function __construct()
    {
        $this->Clientservice = new ArrayCollection();
        $this->Hostingaccount= new ArrayCollection();
    }
    public function getClientservice()
    {
        return $this->Clientservice;
    }
    public function getHostingaccount()
{
    return $this->Hostingaccount;
}

    public function setClientservice($Clientservice)
    {
        $this->Clientservice = $Clientservice;

        return $this;
    }
    public function setHostingaccount($Hostingaccount)
    {
        $this->Hostingaccount = $Hostingaccount;

        return $this;
    }

      

And I have 3 forms:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('CustomerID',IntegerType::class)
        ->add('Sex',TextType::class)
        ->add('Name',TextType::class)
        ->add('FirstName',TextType::class)
        ->add('LastName',TextType::class)
        ->add('Email', EmailType::class)
        ->add('Invoiceemail', EmailType::class)
        ->add('Iban', TextType::class)
        ->add('Bic', TextType::class)
        ->add('SEPAMandateID', TextType::class)
        ->add('LogoUrl', TextType::class)
        ->add('CreationDate', DateType::class)
        ->add('Clientservice', CollectionType::class, array(
            'entry_type' => WhmAdminClientserviceType::class
        ))
        ->add('Hostingaccount', CollectionType::class, array(
            'entry_type' => WhmAdminHostingAccountType::class
        ))
        ->getForm();

}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => Client::class
    ));
}

      

Customer service form

    public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('Description',TextType::class);

}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => Clientservice::class
    ));
}

      

Hosting Form:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('Domain',TextType::class)
        ->add('Domainip',IntegerType::class)
        ->add('UserName',TextType::class)
        ->add('Owner',TextType::class);

}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => Hostingaccount::class
    ));
}

      

In my controller, I have code like this:

 public function AddWhmAdminAction(Request $request)
{

    $Client = new Client();

    $form = $this->createForm(WhmAdminType::class, $Client);
    $form->add('submit', SubmitType::class, array(
        'label' => 'Create',
        'attr' => array('class' => 'btn btn-default pull-left')
    ));
    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {

        $em = $this->getDoctrine()->getManager();

        $em->persist($Client);
        $em->flush();

        $this->get('session')->getFlashBag()->add('green', 'Product created!');
        return $this->redirectToRoute('whmAdmin');
        // flash msg
    }
    //return $form->createView();
    $build['form'] = $form->createView();
    return $this->render('whm/WhmAdminForm.html.twig', $build);

}

      

and in my opinion

{{ form_start(form) }}

{% for flashMessage in app.session.flashbag.get('green') %}

    {{ flashMessage }}

{% endfor %}


{{ form_end(form) }}

      

I followed http://symfony.com/doc/current/form/form_collections.html and this is how far I got. My form only displays fields from the Client. It doesn't display the field from Clientservice and hostingaccount. How can I show fields from customer service and hosting in the same form as Customer.

+3


source to share


1 answer


If Clientservice

u Hostingaccount

are in a OneToOne or ManyToOne relationship, change:

->add('Clientservice', CollectionType::class, array(
    'entry_type' => WhmAdminClientserviceType::class
))
->add('Hostingaccount', CollectionType::class, array(
    'entry_type' => WhmAdminHostingAccountType::class
))

      

in



->add('Clientservice', WhmAdminClientserviceType::class)
->add('Hostingaccount', WhmAdminHostingAccountType::class)

      

Otherwise, leave your form builders and read on adding and removing elements as a collection.

+2


source







All Articles