【问题标题】:Symfony Embed a Collection of Forms multiple tablesSymfony 嵌入多​​个表格的表单集合
【发布时间】:2017-06-09 12:07:19
【问题描述】:

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

托管账户形式:

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 的字段。如何以与客户端相同的形式显示来自客户端服务和托管帐户的字段。

【问题讨论】:

    标签: php forms symfony


    【解决方案1】:

    如果 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 的信息。

    【讨论】:

    • 它给了我这个错误表单的视图数据应该是类 AppBundle\Entity\Clientservice 的实例,但它是类 Doctrine\Common\Collections\ArrayCollection 的实例。您可以通过将“data_class”选项设置为 null 或添加视图转换器来避免此错误,该转换器将类 Doctrine\Common\Collections\ArrayCollection 的实例转换为 AppBundle\Entity\Clientservice 的实例。但是当我将“data_class”更改为 null 时,它确实显示了其他字段。当我持久化数据时,它只会将数据持久化到客户端而不是客户端服务和托管帐户
    • ClientserviceHostingaccount 多对一关系是否与 Client 有关系?
    • 我喜欢这个 /** * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Clientservice", cascade={"persist"}) / protected $Clientservice; /* * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Hostingaccount", cascade={"persist"}) */ protected $Hostingaccount;内部客户实体
    • 所以你需要添加脚本来从原型构建表单集集合。请参阅我的帖子所附的文档。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 2016-07-17
    • 2013-09-16
    • 2011-11-12
    相关资源
    最近更新 更多