Symfony嵌入多个表格的集合

时间:2017-06-09 12:07:19

标签: php forms symfony

我有3个表/实体。我正在尝试创建表单以将数据保存到一个表单中的所有3个表。权利很长,这就是我没有在这里添加的原因。

实体客户端我在客户端实体中为其他2个实体创建了一个数组集合。

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

我有3种形式:

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

客户服务表格

    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表格:

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

在我的控制器中,我有这样的代码:

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

}

在我看来

{{ form_start(form) }}

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

    {{ flashMessage }}

{% endfor %}


{{ form_end(form) }}

我跟着http://symfony.com/doc/current/form/form_collections.html,这就是我得到了多远。 我的表单只显示客户端的字段。它不显示来自Clientservice和hostingaccount的字段。如何以与Client相同的形式显示clientservice和hostingaccount中的字段。

1 个答案:

答案 0 :(得分:2)

如果ClientserviceHostingaccount处于OneToOne或ManyToOne关系中,则更改:

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

为:

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

否则请离开表单构建器并阅读集合类型中的adding and removing items