2015-11-24 101 views
0

我想收集表格。我的砖形的定义如下:如何在Symfony2中为表单集合正确设置'data_class'?

class ImportPaymentsType extends AbstractType 
{ 
    public function getName() 
    { 
     return 'import_payments_type'; 
    } 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('caisse', 'entity', array(
      'label' => null, 
      'class' => 'ACME\AppBundle\Entity\Caisse', 
      'property' => 'name', 
      'empty_value' => 'Choose an option', 
     )) 
     ->add('label', 'text', array(
      'label' => null, 
     )) 
     ->add('credit', 'number', array(
      'label' => null, 
     )); 
    } 
} 

在控制器I构建这种形式,如:

$data = array(
     'imports' => 
      array(
       'caisse' => $em->getRepository('ACMEAppBundle:Caisse')->findOneByCode('P6015C'), 
       'label' => null, 
       'credit' => null, 
      ) 
    ); 

    $importForm = $this->createFormBuilder($data) 
     ->add('imports', 'collection', array(
      'type' => new ImportPaymentsType(), 
     ))->getForm(); 

我得到的错误是:

表单的视图数据有望成为类型标量,数组或者\ ArrayAccess的实例,但是是类ACPI \ AppBundle \ Entity \ Caisse类的一个实例。 ACME \ AppBundle \ Entity \ Caisse。可以通过 避免这种错误设置“data_class”选项 “ACME \ AppBundleBundle \实体\储蓄银行”或通过添加视图 变压器该变换类 ACME \ AppBundleBundle \实体\储蓄银行的实例,以标量,数组或\ ArrayAccess的实例 。

我试图改变ImportPaymentsType,但它没有工作:

->add('caisse', 'entity', array(
     'label' => null, 
     'class' => 'ACME\AppBundle\Entity\Caisse', 
     'property' => 'description', 
     'empty_value' => 'Choose an option', 

     'data_class' => 'ACME\AppBundle\Entity\Caisse' 

    )) 

我该怎么办?请帮忙。

回答

2

'imports'应该是一个数组的数组。

尝试:

$data = array(
    'imports' => 
     array(
      array(
       'caisse' => $em->getRepository('ACMEAppBundle:Caisse')->findOneByCode('P6015C'), 
       'label' => null, 
       'credit' => null, 
      ) 
     ) 
); 
+0

多么愚蠢的我 – VladRia