Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用FormBuilder将实体属性的表单字段拆分为多个子字段_Php_Symfony_Doctrine Orm_Formbuilder - Fatal编程技术网

Php 使用FormBuilder将实体属性的表单字段拆分为多个子字段

Php 使用FormBuilder将实体属性的表单字段拆分为多个子字段,php,symfony,doctrine-orm,formbuilder,Php,Symfony,Doctrine Orm,Formbuilder,我有一个超类,它被两个实体扩展。超类与另一个实体有N:M关系。在后者的formbuilder中,我需要将这两个子类型划分为不同的字段 这是它的FormType::buildForm方法: public function buildForm(formbuilder接口$builder,数组$options) { $builder ->添加('选项\子类型\ 1','实体'[ 'class'=>'AcmeExampleBundle:Options', “扩展”=>true, “多个”=>true,

我有一个超类,它被两个实体扩展。超类与另一个实体有N:M关系。在后者的formbuilder中,我需要将这两个子类型划分为不同的字段

这是它的FormType::buildForm方法:

public function buildForm(formbuilder接口$builder,数组$options)
{
$builder
->添加('选项\子类型\ 1','实体'[
'class'=>'AcmeExampleBundle:Options',
“扩展”=>true,
“多个”=>true,
“属性路径”=>“选项”,
“查询生成器”=>/*特定筛选器1*/
])
->添加('选项\子类型\ 2','实体'[
'class'=>'AcmeExampleBundle:Options',
“扩展”=>true,
“多个”=>true,
“属性路径”=>“选项”,
“查询生成器”=>/*特定筛选器2*/
])
;
}
但是,表单提交时不会保存任何选项,除非我对上面两个字段中的一个进行注释(左侧字段已保存)。我想象一下,将同一表单上的两个字段中的
property\u path
设置为同一属性不起作用


我怎样才能做到这一点

我仍在自己寻找完美的解决方案。目前,我使用映射的隐藏字段和PRE_SUBMIT Form事件来处理此类问题。这看起来像

FormType::buildForm

public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $builder
         ->add('options_subtype_1', 'entity', [
             'class' => 'AcmeExampleBundle:Options',
             'expanded' => true,
             'multiple' => true,
             'mapped'=> false,
             'data' => $builder->getData()->getOptions(),
             'query_builder' => /* specific filter 1 */
         ])
         ->add('options_subtype_2', 'entity', [
             'class' => 'AcmeExampleBundle:Options',
             'expanded' => true,
             'multiple' => true,
             'mapped' => false,
             'data' => $builder->getData()->getOptions(),
             'query_builder' => /* specific filter 2 */
         ])
         ->add('options', 'entity', [
             'class' => 'AcmeExampleBundle:Options',
             'required' => false,
             'multiple' => true,
             'attr' => ['style' => 'visibility: hidden;'],
             'label' => false
         ]);

    $builder->addEventListener(
        FormEvents::PRE_SUBMIT,
        function (FormEvent $event) {
            $data = $event->getData();
            unset($data['options']);
            $data['options'] = array_merge($data['options_subtype_1'], $data['options_subtype_2']);
            $event->setData($data);
        }
    );
}