在Symfony中使用联接创建表单2

时间:2022-10-22 18:13:25

Now I'm trying to create Forms with the Symfony 2 Formbuilder. I have a Table "Client" with a join on the Table "Address".

现在我正在尝试使用Symfony 2 Formbuilder创建表单。我有一个表“客户”与表“地址”上的连接。

I want a form with the fields of Client and some fields of Address.

我想要一个包含Client字段和一些Address字段的表单。

All the fields should be Textfields.

所有字段都应该是Textfields。

Thats what im done so far, but I'm wondering if this is really the best practice or if theres an better way to solve this.

多数民众赞成到目前为止我做了什么,但我想知道这是否真的是最好的做法,或者如果这是一个更好的解决方法。

I created for the table address an extra type and added it in the client type.

我为表地址创建了一个额外的类型,并将其添加到客户端类型中。

The Client Type is used for the FormBuilder.

客户端类型用于FormBuilder。

AddressType.php

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('street')
        ->add('company');
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Test\UserBundle\Entity\Address',
    ));
}

public function getName()
{
    return 'Address';
}

ClientType.php

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('title')
        ->add('Address', new AddressType());
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Test\UserBundle\Entity\Client',
    ));
}

public function getName()
{
    return 'Client';
}

1 个解决方案

#1


For me, it looks like the best practice, which consists in using embedded forms. You can find more about that here :

对我来说,它看起来是最佳实践,包括使用嵌入式表单。你可以在这里找到更多相关信息:

http://symfony.com/doc/current/book/forms.html#embedded-forms

#1


For me, it looks like the best practice, which consists in using embedded forms. You can find more about that here :

对我来说,它看起来是最佳实践,包括使用嵌入式表单。你可以在这里找到更多相关信息:

http://symfony.com/doc/current/book/forms.html#embedded-forms