Forms Symfony3表单实体类型

Forms Symfony3表单实体类型,forms,symfony,Forms,Symfony,我正在使用Symfony3,为EntityType::class创建表单时出现以下错误 我的实体名称空间 项目\CoreBundle\Entity\CountryEntity public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('name', TextType::class, ['label' => 'Name *', 'attr'

我正在使用Symfony3,为EntityType::class创建表单时出现以下错误

我的实体名称空间 项目\CoreBundle\Entity\CountryEntity

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', TextType::class, ['label' => 'Name *', 'attr' => ['data-help'  => 'enter language name here.']])
        ->add('iso', TextType::class, ['label' => 'ISO *', 'attr' => ['data-help'  => 'enter language code here.']])
        ->add('defaultLanguage', EntityType::class,
            array(
                'class' => 'Project\CoreBundle\Entity\CountryEntity'
            )
        )
        ->getForm();
    ;
}
抛出可捕获的致命错误:无法将类Project\CoreBundle\Entity\CountryEntity的对象转换为字符串


任何帮助都会很有用,谢谢

因为EntityType是ChoiceType的扩展,它需要知道如何将实体呈现到表单中。如果不传递任何信息,它将尝试使用
\uu toString()
方法。如果未定义,则会出现此错误

您可以为字段指定
choice\u label
选项,而不是实现
\u toString()
,该选项应该是应该显示的属性的路径规范。例如,您的
CountryEntity
类可能具有
名称
属性:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', TextType::class, ['label' => 'Name *', 'attr' => ['data-help'  => 'enter language name here.']])
        ->add('iso', TextType::class, ['label' => 'ISO *', 'attr' => ['data-help'  => 'enter language code here.']])
        ->add('defaultLanguage', EntityType::class,
            array(
                'class' => 'Project\CoreBundle\Entity\CountryEntity',
                'choice_label' => 'name'
            )
        )
        ->getForm();
    ;
}
另请参见位于的EntityType的文档


最好是

由于EntityType是ChoiceType的扩展,它需要知道如何将实体呈现到表单中。如果不传递任何信息,它将尝试使用
\uu toString()
方法。如果未定义,则会出现此错误

您可以为字段指定
choice\u label
选项,而不是实现
\u toString()
,该选项应该是应该显示的属性的路径规范。例如,您的
CountryEntity
类可能具有
名称
属性:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', TextType::class, ['label' => 'Name *', 'attr' => ['data-help'  => 'enter language name here.']])
        ->add('iso', TextType::class, ['label' => 'ISO *', 'attr' => ['data-help'  => 'enter language code here.']])
        ->add('defaultLanguage', EntityType::class,
            array(
                'class' => 'Project\CoreBundle\Entity\CountryEntity',
                'choice_label' => 'name'
            )
        )
        ->getForm();
    ;
}
另请参见位于的EntityType的文档

致意