Symfony2无法加载类型EntityType

Mik*_*ról 1 php doctrine symfony

当我尝试在Symfony2表单类型中创建EntityType字段时,我遇到了问题.

这是Symfony2手册中官方EntityType参考的一段代码:

use Symfony\Bridge\Doctrine\Form\Type\EntityType; 

...

$builder->add('users', EntityType::class, array(
        'class' => 'AppBundle:User',
        'choice_label' => 'username',
    ));
Run Code Online (Sandbox Code Playgroud)

回到浏览器我收到Could not load type "Symfony\Bridge\Doctrine\Form\Type\EntityType"错误.

EntityType类存在于给定的namespespace中.

我是否需要为此字段类型获取一些附加扩展名?

Xat*_*too 10

您使用的是什么版本的Symfony?

FooBarType::class是Symfony 2.8和3.x中的新功能,旧版本不支持它.尝试更换EntityType::class'entity',看看是否有效.当您升级到Symfony 2.8时,您可以使用FQCN,从3.x这将是唯一的选择.


Rou*_*ubi 8

EntityType类不在常见的Type类文件夹(Symfony\Component\Form\Extension\Core\Type\)中,而是在Symfony\Bridge\Doctrine\Form\Type\

因此,如果你使用Symfony 3或Symfony 2.8和EntityType::class语法,你必须把

use Symfony\Bridge\Doctrine\Form\Type\EntityType;
Run Code Online (Sandbox Code Playgroud)