简体   繁体   English

Symfony2表单构建器添加实体“无法加载类型实体”

[英]Symfony2 Form builder add entity 'Could not load type entity'

I've created a form type in Symfony that extends the Abstract type, and added the fields using the builder, but no matter what I do it won't work! 我已经在Symfony中创建了一个扩展了Abstract类型的表单类型,并使用了构建器添加了字段,但是无论我做什么都行不通!

class MyType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', 'text');
        $builder->add('other', 'entity', array(
            'data_class' => 'My\App\DefaultBundle\Entity\Other'
        ));
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'runSpeeds' => 'My\App\DefaultBundle\Entity\My',
        ));
    }

    public function getName()
    {
        return 'my';
    }
}

Could not load type "entity" 500 Internal Server Error - InvalidArgumentException 无法加载类型“实体” 500内部服务器错误-InvalidArgumentException

"My" Entity has a column which references the id of "Other" using a foreign key constraint. “我的”实体具有使用外键约束引用“其他”的ID的列。 I want my form to basically have a drop down in the form for "My" that displays all the values from the "name" column in the "Other" entity using the Other.id -> My.other_id as reference. 我希望我的表单在“ My”的表单中有一个下拉列表,该表单使用Other.id-> My.other_id作为引用显示“ Other”实体中“ name”列中的所有值。

Update 更新资料

I have an OtherType (Form type) and the following will work: 我有一个OtherType(表单类型),以下将起作用:

$builder>add('name', new OtherType(), array(
    'data_class' => 'My\App\DefaultBundle\Entity\Other')
)

But this displays the entire entity in the form. 但这会以表格形式显示整个实体。 I only want one field from the Other entity to display, and in a dropdown with the choices 我只希望显示“其他”实体中的一个字段,并在下拉菜单中显示选项

You didn't provide the required option class . 您没有提供必需的选项class

As mentioned in the documentation of entity Field Type 实体字段类型的文档中所述

EDIT: 编辑:

Moreover you have two syntax issues (" ; " is missing) 此外,您有两个语法问题(“ ”缺失)

$builder->add('name', 'text')
$builder->add('other', 'entity', array(
    'data_class' => 'My\App\DefaultBundle\Entity\Other'
))

Use the required class attribute, as defined in the basic usage http://symfony.com/doc/current/reference/forms/types/entity.html#basic-usage 使用基本用法http://symfony.com/doc/current/reference/forms/types/entity.html#basic-usage中定义的必需的class属性

$builder->add('other', 'entity', array(
    'class' => 'DefaultBundle:Other'
))

If your Other class implements a __toString() method you can use that to determine the label. 如果您的Other类实现__toString()方法,则可以使用该方法来确定标签。 You can also use property for that: 您还可以为此使用property

$builder->add('other', 'entity', array(
    'class' => 'DefaultBundle:Other',
    'property' => 'name',
))

You need to add this: 您需要添加以下内容:

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'My\App\DefaultBundle\Entity\My'
    ));
}

And change data_class to class in the following lines: 并在以下几行data_class更改为class

    $builder->add('other', 'entity', array(
        'data_class' => 'My\App\DefaultBundle\Entity\Other'
    ));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM