Symfony2如何创建多维集合字段

时间:2014-11-18 02:54:29

标签: php symfony

如何使用symfony2创建一个多维集合字段,如下所示:

array [
   group_1=>[index_1=>value_1, index_2=value_2], 
   group_2=>[
             group_3=>[index_1=>value_1]
             index_1=value_1
            ]
  ]

我的问题是如何用像

这样的表单类型来表示这个数据格式
public function buildForm ( FormBuilderInterface $builder, array $options )
{

    $fields = $this->form->getFieldset($fieldset->name);

    if ( !empty($fields) ) {

        foreach ( $this->fields as $field ) {

        $groups = explode('.', $field->group); // the result in the form should be like this fieldnamegroup[group1][group2]

        foreach ($groups as $group)
            {
                $builder->add($group, 'collection' array('type'=>"another collection type for making a cascade of subgroup" ));

            }

           $builder->add($field->__get('name'), new FieldType($field)); // final type


        }
    }
}

我想创建另一个表单类型Group扩展CollectionType但我在论坛中读到了不可能的

我需要显示的最终结果字段就像

<input name="FieldControl[Group1][Group2][Group3]" value = "value from the Field type object">

1 个答案:

答案 0 :(得分:1)

这是php问题:

$array = array(
    "foo" => "bar",
    42    => 24,
    "multi" => array(
         "dimensional" => array(
             "array" => "foo"
         )
    )
);

检查php documentation