繁体   English   中英

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

[英]Catchable Fatal Error: Argument 1 passed to "…\FormType::__construct() must implement interface

我想打电话entityManagerformType 我不明白为什么这不起作用。

表格类型:

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行并定义

假设正在加载services.yml文件,并且复制粘贴内容,那么您将遇到一个简单的错字:

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

让我们看看你的错误

参数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);

您将表单定义为服务(在services.yml ),但没有将其用作服务。 您应该使用service container来创建表单,而不是createForm ,因此请更改:

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

变成:

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

并阅读有关将表单定义为服务的更多信息

尝试使用services.yml:

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

Symfony的3.4

暂无
暂无

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

相关问题 Symfony2:ContextErrorException:可捕获的致命错误:传递给[…] :: __ construct()的参数1必须实现接口[…]没有给出 可捕获的致命错误:传递给Foo :: bar()的参数1必须实现接口BazInterface,null给出 切换表时出现zend db错误可捕获的致命错误:传递给__construct()的参数1必须是一个数组,给定对象,在 可捕获的致命错误:传递给UserBundle \\ Form \\ UserType :: __ construct()的参数2必须是实例? 可捕获的致命错误:传递给AppBundle \\ Form \\ TagType :: __ construct()的参数1必须是Doctrine \\ ORM \\ EntityRepository的实例,未给出任何实例, 可捕获的致命错误:传递给Controller :: __ construct()的参数1必须是Doctrine \\ ORM \\ EntityManager的实例,未给出任何实例,称为 Symfony 2可捕获的致命错误:传递给Sg \\ DatatablesBundle \\ Datatable \\ :: __ construct()的参数1必须是的实例 可捕获的致命错误:传递给 Illuminate\\Config\\Repository::__construct() 的参数 1 必须是数组类型,给定整数 可捕获的致命错误:传递给UsernamePasswordToken :: __ construct()的参数4必须是一个数组,给定null 可捕获的致命错误:传递给 Album\Controller\AlbumController::__construct() 的参数 1 必须是 Album\Model\AlbumTable 的实例,没有给出
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM