![](/img/trans.png)
[英]Symfony2: ContextErrorException: Catchable Fatal Error: Argument 1 passed to […]::__construct() must implement interface […] none given
[英]Catchable Fatal Error: Argument 1 passed to "…\FormType::__construct() must implement interface
我想打电话entityManager
在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行并定义
假设正在加载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.