Symfony2在FormType中注入EntityMananager [英] Symfony2 inject EntityMananager in FormType

查看:60
本文介绍了Symfony2在FormType中注入EntityMananager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自定义表单类型


Custom form type

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityManager;

class NationaliteitidType extends AbstractType 
{
private $doctrine;
private $em;

public function __construct(EntityManager  $em)
{
    $this->em = $em;
}

service.yml
服务:

service.yml services:

fw_core.form.type:
    class: FW\CoreBundle\Form\Type\NationaliteitidType
    arguments: 
        entityManager: "@doctrine.orm.entity_manager"

错误:


传递给FW\CoreBundle\Form\TypeNationaliteitidType :: __ construct()的参数1必须是Doctrine\ORM\EntityManager的实例,没有给出,

Argument 1 passed to FW\CoreBundle\Form\TypeNationaliteitidType::__construct() must be an instance of Doctrine\ORM\EntityManager, none given,

我必须已经创建了某种类型或其他明显的类型,但确实找不到它。

I must have made an type or something else obvious but realy can't find it.

推荐答案

在您的services.yml中,您无法命名将来的变量,因此请尝试如下操作:

In your services.yml, you can't name your future variables, so try something like this :

services :
    fw_core.form.type:
        class: FW\CoreBundle\Form\Type\NationaliteitidType
        arguments: 
            - "@doctrine.orm.entity_manager"

这篇关于Symfony2在FormType中注入EntityMananager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆