获得以下错误..
在渲染模板期间抛出异常("表单的视图数据应该是标量,数组或\ ArrayAccess的实例,但是类Proxies__CG__的实例\ BLA \ MyBundle \ Entity \ TransportType。您可以通过将" data_class"选项设置为" Proxies _CG __ \ BLA \ MyBundle \ Entity \ TransportType"或者添加一个视图转换器来避免此错误将类Proxies__CG __ \ BLA \ MyBundle \ Entity \ TransportType的实例转换为MyBundle中的标量,数组或\ ArrayAccess的实例。")在第8行中运输:form.html.twig。
$builder->add('variables','collection', array(
'type' => new AbcType(),
'options' => array(
'required' => true,
),
'constraints' => new NotNull()));
AbcType.php
class AbcType extends AbstractType
{
/**
* Build form
*
* @param FormBuilder $builder
* @param array $options
*
* @return void
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('importance', null, array('empty_value'=>false,'expanded'=>true,
'required'=>true,'multiple'=>false,
'constraints' => new NotNull()))
->add('timeSpent', null, array(
'empty_value'=>false,'expanded'=>true,
'required'=>true,'multiple'=>false,
'constraints' => new NotNull()
)
);
}
/**
* setDefaultOptions set Default values
*
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Demo\MyBundle\Entity\Abc'
));
}
/**
* getName will return Form name
* @return string
*/
public function getName()
{
return 'demo_mybundle_abctype';
}
}
答案 0 :(得分:1)
我使用以下链接解决了问题......
答案 1 :(得分:0)
确保您拥有' data_class'在AbcType类的setDefaultOptions中指定的默认值:
... build form ...
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AbcBundle\Entity\Abc'
));
}