luffy
luffy

Reputation: 55

Symfony Embed a Collection of Forms multiple tables

I have 3 tables/entities. I am trying to create form to persist data to all 3 table in one form. Entitities are to long that is why I haven't added here.

Entity Client I made an array collection in Client entity for other 2 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
    ));
}

Clientservice 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
    ));
}

Hostingaccount 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 the 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 view

{{ 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 shows the fields from Client. It does not shows field from Clientservice and hostingaccount. How can I show the fields from clientservice and hostingaccount in the same form as Client.

Upvotes: 3

Views: 1215

Answers (1)

miikes
miikes

Reputation: 984

If the Clientservice and Hostingaccount are in OneToOne or ManyToOne relationship then change:

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

to:

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

Otherwise leave your form builders and read about adding and removing items in collection type.

Upvotes: 2

Related Questions