可捕获的致命错误:传递给“... \ FormType :: __ construct()的参数1必须实现接口

时间:2015-12-01 13:12:01

标签: php symfony doctrine-orm

我想在entityManager中致电formType。我不明白为什么这不起作用。

FormType:

private $manager;

public function __construct(ObjectManager $manager)
{
    $this->manager = $manager;
}

控制器:

$form = $this->createForm(ProductsType::class, $products);

服务:

apx.form.type.product:
    class: ApxDev\UsersBundle\Form\ProductType
    arguments: ["@doctrine.orm.entity_manager"]
    tags:
        - { name: form.type }

错误:

  

捕获致命错误:传递给MyBundle \ Form \ FormType :: __ construct()的参数1必须实现接口Doctrine \ Common \ Persistence \ ObjectManager,没有给出,在vendor / symfony / symfony / src / Symfony / Component / Form中调用/FormRegistry.php在第90行并定义了

4 个答案:

答案 0 :(得分:3)

假设您的services.yml文件正在加载并且您复制粘贴在内容中,那么您有一个简单的拼写错误:

# services.yml
class: ApxDev\UsersBundle\Form\ProductType
should be
class: ApxDev\UsersBundle\Form\ProductsType

答案 1 :(得分:1)

让我们看看你的错误

  

参数1传递给MyBundle \ Form \ FormType :: __ construct()

因此,当您实例化FormType时,我们正在谈论您通过的论点

$form = new \MyBundle\Form\FormType($somearg);

你的定义是

public function __construct(ObjectManager $manager)

基于错误的第二部分

  

必须实现接口Doctrine \ Common \ Persistence \ ObjectManager

很明显,ObjectManager是一个界面。那么这意味着你必须在你注入你的类的对象中实现该接口,因为那是你告诉PHP所期望的。这看起来是什么

class Something implements \Doctrine\Common\Persistence\ObjectManager {
    // Make sure you define whatever the interface requires within this class
}
$somearg = new Something();
$form = new \MyBundle\Form\FormType($somearg);

答案 2 :(得分:1)

您已将表单定义为服务(在services.yml中),但您并未将其用作服务。您应该使用createForm来创建表单,而不是service container,所以请更改:

$form = $this->createForm(ProductsType::class, $products);

成:

$form = $this->get('apx.form.type.product')

并详细了解defining forms as a service

答案 3 :(得分:0)

尝试使用services.yml:

apx.form.type.product:
    class: ApxDev\UsersBundle\Form\ProductType
    arguments: 
        - '@doctrine.orm.entity_manager'
    tags:
        - { name: form.type }

Symfony 3.4

相关问题