Symfony 将entityManager传递给EntityType类

Symfony 将entityManager传递给EntityType类,symfony,entitymanager,Symfony,Entitymanager,使用Symfony 3.2,我将通过构造函数将entityManager对象传递给EntityType类 我发现可以使用服务来完成,如下所示: config.yml 服务.yml 我的实体类型: namespace App\Bundle\mybundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Opt

使用Symfony 3.2,我将通过构造函数将entityManager对象传递给EntityType类

我发现可以使用服务来完成,如下所示:

config.yml

服务.yml

我的实体类型:

namespace App\Bundle\mybundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Doctrine\ORM\EntityManager;

class ProfileKeyType extends AbstractType
{

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

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder->add('key', TextType::class, array(
                "label" => "Chiave identificativa"
        ))->add('visible', CheckboxType::class, array(
                'label' => "Default visible",
                "label_attr" => array( 'title' => 'Imposta la visibilità di default di questa chiave' ),
                'required' => true
        ))->add('entity', CollectionType::class, array(
                "data_class" => Entity::class
        ))->add('property')->add('type');
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'App\Bundle\mybundle\Entity\ProfileKey'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'app_bundle_mybundle_profilekey';
    }


}
当我启动插入页时,出现以下错误:

Type error: Argument 1 passed to App\Bundle\myBundle\Form\ProfileKeyType::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called in D:\eclipse_neon_workspace\iSerPa\vendor\symfony\symfony\src\Symfony\Component\Form\FormRegistry.php on line 85
怎么了


Thx提前

您应该将form.type标记添加到服务定义中,如下所示:

services:
serpabackend.profile_key:
    class: App\Bundle\mybundle\Form\ProfileKeyType
    arguments: ["@doctrine.orm.entity_manager"]
    tags:
        - { name: form.type }

您应该向服务定义中添加form.type标记,如下所示:

services:
serpabackend.profile_key:
    class: App\Bundle\mybundle\Form\ProfileKeyType
    arguments: ["@doctrine.orm.entity_manager"]
    tags:
        - { name: form.type }

创建表单时,您只需执行以下操作:

// inside controller action
// $em is the required EntityManager instance
$em = $this->getDoctrine()->getEntityManager();
$form = $this->createForm(new ProfileKeyType($em), $profileKey);

创建表单时,您只需执行以下操作:

// inside controller action
// $em is the required EntityManager instance
$em = $this->getDoctrine()->getEntityManager();
$form = $this->createForm(new ProfileKeyType($em), $profileKey);


这是一个类型问题,参数中使用的类型可能不是由
use
语句导入的类型。您的意思是ProfileKeyType“use-Doctrine\ORM\EntityManager”中使用的对象与传递的服务不同。yml??是的,这就是我的意思,但我看到的是
@doctor.orm.entity\u manager
代表
doctor\orm\EntityManager
,这很奇怪。如果您有ita,请尝试使用其他entityManager)如果尚未清除缓存,请清除缓存。b) 使用该表单类型发布控制器,确保您没有尝试自己直接实例化该类。(例如,通过带有类名的createForm,在这种情况下使用服务名)这是一个类型问题,参数中使用的类型可能不是
use
语句导入的类型。您的意思是ProfileKeyType“use Doctrine\ORM\EntityManager”中使用的对象与通过服务传递的对象不同。yml??是的,这就是我的意思,但我看到的是
@doctor.orm.entity\u manager
代表
doctor\orm\EntityManager
,这很奇怪。如果您有ita,请尝试使用其他entityManager)如果尚未清除缓存,请清除缓存。b) 使用该表单类型发布控制器,确保您没有尝试自己直接实例化该类。(例如,通过带有类名的createForm,在这种情况下使用服务名)未解决!symfony要求我做的唯一一件事是将类从class:App\Bundle\mybundle\Form\ProfileKeyType更改为class:Appmybundle\Form\ProfileKeyType,但错误是相同的:类型错误:传递给App\Bundle\mybundle\Form\ProfileKeyType的参数1::\uu construct()必须是doctor\ORM\EntityManager的实例,没有给出,在第85行的D:\eclipse\u neon\u workspace\iSerPa\vendor\symfony\symfony\src\symfony\Component\Form\FormRegistry.php中调用。如何创建表单?使用CRUD实用程序,在生成的控制器ProfileKeyController$profileKey=new profileKey()中$form=$this->createForm('App\Bundle\myBundle\form\ProfileKeyType',$profileKey)$表单->HandlerRequest($request)。。。。返回$this->render('profilekey/new.html.twig',数组('profilekey'=>$profilekey,'form'=>$form->createView(),);}尝试在controller中获取服务,并查看$this->get('serpabackend.profile_key')是否出现错误;您还可以检查您的服务是否正确加载了bin/console | grep“serpabackend.profile_key”省略了console命令的一部分。bin/console debug:container serpabackend,是的,似乎没有创建服务。我想你也会尝试清除缓存,不过只要你处于开发模式,这就不重要了。没有解决!symfony要求我做的唯一一件事是将类从class:App\Bundle\mybundle\Form\ProfileKeyType更改为class:Appmybundle\Form\ProfileKeyType,但错误是相同的:类型错误:传递给App\Bundle\mybundle\Form\ProfileKeyType的参数1::\uu construct()必须是doctor\ORM\EntityManager的实例,没有给出,在第85行的D:\eclipse\u neon\u workspace\iSerPa\vendor\symfony\symfony\src\symfony\Component\Form\FormRegistry.php中调用。如何创建表单?使用CRUD实用程序,在生成的控制器ProfileKeyController$profileKey=new profileKey()中$form=$this->createForm('App\Bundle\myBundle\form\ProfileKeyType',$profileKey)$表单->HandlerRequest($request)。。。。返回$this->render('profilekey/new.html.twig',数组('profilekey'=>$profilekey,'form'=>$form->createView(),);}尝试在controller中获取服务,并查看$this->get('serpabackend.profile_key')是否出现错误;您还可以检查您的服务是否正确加载了bin/console | grep“serpabackend.profile_key”省略了console命令的一部分。bin/console debug:container serpabackend,是的,似乎没有创建服务。我想你也会尝试清除缓存,不过只要你处于开发模式,这就不重要了。嗨,Stephane,我在其他讨论中见过这个解决方案,但我一直认为这是一个解决办法。我相信注射是最好的解决方案,但如果它不起作用,我想我会打开你的解决方案。RgdsHi,如果您想使用注入,您可以像symfony中的另一个服务一样获取formType实例,我的意思是:$form=$this->get('mybundle.profile_key');对不起,Stephan,您的意思是,在我的ProfileKeyType中,我必须从$this->get(“serpabackendbundle.profile_key”)获取ProfileKeyType吗??我认为这是一个没有意义的代码,因为我需要在ProfileKeyType中使用entity_manager来创建属性类型字段,并带有select元素和相关实体选项否,我的意思是,当您在controller中创建表单时,如果您不想使用新的ProfileKeyType($em),您可以使用:$formType=$this->get获得formType(“serpabackendbundle.profile_key”),然后使用:$form=$this->createForm($formType,$entity)创建表单啊,好的,Stephan,我会试试!ThxHi Stephane,我在其他讨论中见过这个解决方案,但我一直认为它是一个解决办法。我相信注射是最好的解决方案,但如果它不起作用,我想我会打开你的解决方案。Rgd