我需要修改表单中的一些字段(标签和类),这取决于实体是否是最新发布的版本。因此,我需要能够将实体管理器注入到我的formtype中,以便在事件侦听器中可以将当前版本与实体的已发布版本进行比较。但我甚至不能从entityManager
开始进入到u构造中。也许还有更好的方法来实现我的大目标(例如,在twig模板中修改表单),但是我需要了解如何进行这个基本的依赖注入。
我认为,如果我在服务中声明它(就像basicService Container和特别是Constructor Injection方法的文档所描述的那样),它将作为参数在我的构造中可用。但是当我这样做的时候,我得到了一个错误:Catchable fatal error: Argument 1 passed to Gutensite\CmsBundle\Form\Type\ViewType::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called in /var/www/core/cms/src/Gutensite/ArticleBundle/Controller/AdminEditController.php on line 222 and defined in /var/www/core/cms/src/Gutensite/CmsBundle/Form/Type/ViewType.php on line 15
以下是我的代码片段:
gutensite/cmsbundle/resources/config/service.yml
gutensite_cms.form.type.view:
class: Gutensite\CmsBundle\Form\Type\ViewType
arguments: [ "@doctrine.orm.entity_manager" ]
gutensite/cmsbundle/form/type/viewtype.php
namespace Gutensite\CmsBundle\Form\Type;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\AbstractType;
class ViewType extends AbstractType
{
private $em;
public function __construct(EntityManager $entityManager) {
$this->em = $entityManager;
}
}
gutensite/articlebundle/controller/adminiditcontroller.php
// Get the View Entity
$em = $this->getDoctrine()->getManager();
$viewRepo = $em->getRepository("GutensiteCmsBundle:View\View");
$view = $viewRepo->find($request->query->get('id'));
// Create the generic form for editing any View, using the view entity constructed
$form = $this->createForm(new ViewType(), $view);
注:
我使用两个实体管理器,所以我的config.yml看起来像这样。我不知道这是否会对我注射的东西产生影响,即我可以注射
@doctrine.orm.entity_manager
还是应该注射@doctrine.orm.default_entity_manager
或其他东西?我试过各种各样的选择,但都没有成功。# Doctrine Configuration
doctrine:
dbal:
default_connection: cms
connections:
cms:
driver: "%db.cms.driver%"
host: "%db.cms.host%"
port: "%db.cms.port%"
dbname: "%db.cms.name%"
user: "%db.cms.user%"
password: "%db.cms.password%"
charset: "%db.cms.charset%"
billing:
driver: "%db.billing.driver%"
host: "%db.billing.host%"
port: "%db.billing.port%"
dbname: "%db.billing.name%"
user: "%db.billing.user%"
password: "%db.billing.password%"
charset: "%db.billing.charset%"
orm:
default_entity_manager: cms
entity_managers:
cms:
connection: cms
mappings:
GutensiteCmsBundle: ~
GutensiteArticleBundle: ~
billing:
connection: billing
mappings:
GutensiteBillingBundle: ~
auto_generate_proxy_classes: "%kernel.debug%"
已引用:
Symfony 2 EntityManager injection in service
Symfony2 inject EntityMananager in FormType
解决方案:
我不需要将viewtype定义为服务,只需要在创建新viewtype表单时通过
new viewType($em)
传入实体管理器:gutensite/articlebundle/controller/adminiditcontroller.php
// Get the View Entity
$em = $this->getDoctrine()->getManager();
$viewRepo = $em->getRepository("GutensiteCmsBundle:View\View");
$view = $viewRepo->find($request->query->get('id'));
// Create the generic form for editing any View, using the view entity constructed
$form = $this->createForm(new ViewType($em), $view);
最佳答案
出现此错误是因为您正在创建这样的表单类型:
$form = $this->createForm(new ViewType(), $view);
您创建的新对象
ViewType
没有任何参数,需要用EntityManager
调用它。您只需从控制器传递实体管理器,如下所示:$em = $this->get('doctrine.orm.entity_manager'); // or doctrine.orm.billing_entity_manager
$form = $this->createForm(new ViewType($em), $view);
在这种情况下,您甚至不需要将此表单类型定义为服务。
使用
doctrine.orm.entity_manager
或doctrine.orm.billing_entity_manager
取决于需要从ViewType
-内部(从witch数据库)获取什么。更新:
将表单类型定义为服务。
将这两个服务添加到配置中(
services.yml
):services
gutensite_cms.form.view:
factory_method: createNamed
factory_service: form.factory
class: Symfony\Component\Form\Form
arguments:
- view_form # name of the form
- view # alias of the form type
- null # data to bind, this is where your entity could go if you have that defined as a service
- { validation_groups: [Default] } # validation groups
gutensite_cms.form.type.view:
class: Gutensite\CmsBundle\Form\Type\ViewType
arguments: [ "@doctrine.orm.entity_manager" ]
tags:
- { name: form.type, alias: view }
然后,您可以通过在控制器(或任何具有
container
的对象)内执行此操作来创建新表单,而无需手动传递任何参数(它们将自动注入):public function newAction()
{
$view = ...;
$form = $this->get( 'gutensite_cms.form.view' );
// set initial form data if needed
$form->setData( $view );
}