我正在尝试调用formType
中的entityManager
。我不明白为什么这不起作用。
表单类型:
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,未给定接口,在第90行的vendor/symfony/symfoy/src/symfony/Component/Form/FormRegistry.php中调用并定义
假设你的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)
基于错误的第二部分
必须实现接口条令''Common''Persistence''ObjectManager
CCD_ 4显然是一个接口。因此,这意味着您必须在注入类的对象中实现该接口,因为这正是您告诉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')
并阅读有关将表单定义为服务的更多信息
试用服务。yml:
apx.form.type.product:类:ApxDev''UsersBundle''Form''ProductType自变量:-'@doctrine.org.mentity_manager'标记:-{name:form.type}
Symfony 3.4