【问题标题】:pass entityManager to EntityType class将 entityManager 传递给 EntityType 类
【发布时间】:2017-05-20 14:21:35
【问题描述】:

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

我发现可以使用服务来完成,如下我的配置:

config.yml

imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: services.yml }
    - { resource: "@mybundle/Resources/config/services.yml" }

services.yml

services:
    mybundle.profile_key:
        class: App\Bundle\mybundle\Form\ProfileKeyType
        arguments: ["@doctrine.orm.entity_manager"]

我的实体类型:

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

怎么了??

提前谢谢

【问题讨论】:

  • 是类型问题,参数中使用的类型可能不是use语句导入的类型。
  • 你的意思是ProfileKeyType“使用Doctrine\ORM\EntityManager”中使用的对象与通过services.yml传递的不一样吗??
  • 是的,这就是我的意思,但我看到的是@doctrine.orm.entity_manager 代表Doctrine\ORM\EntityManager,这很奇怪。如果你有,请尝试使用另一个 entityManager
  • a) 如果您还没有这样做,请清除您的缓存。 b)使用该 FormType 发布控制器,确保您没有尝试自己直接实例化该类。 (例如,通过带有类名的 createForm,在这种情况下使用服务名)

标签: symfony entitymanager


【解决方案1】:

创建表单时,您可以这样做:

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

【讨论】:

  • 嗨 Stephane,我在其他讨论中看到了这个解决方案,但我一直认为它是一种解决方法。我相信注入是最好的解决方案,但如果它不起作用,我想我会打开你的解决方案。 Rgds
  • 嗨,如果你想使用注入,你可以像 symfony 中的另一个服务一样获取你的 formType 实例,我的意思是:$form = $this->get('mybundle.profile_key');
  • 对不起 Stephan,你的意思是说,在我的 ProfileKeyType 中,我必须从 $this->get("serpabackendbundle.profile_key") 获取 ProfileKeyType?我认为这是一个没有意义的代码,因为我需要在 ProfileKeyType 中使用 entity_manager 来创建带有选择元素和相关实体选项的属性类型字段
  • 不,我的意思是当你在控制器中创建表单时,如果你不想使用 new ProfileKeyType($em),你可以通过以下方式获取 formType:$formType = $this->get ("serpabackendbundle.profile_key"),然后使用以下命令创建表单:$form = $this->createForm($formType, $entity)
  • 啊好的斯蒂芬,我会试试的!谢谢
【解决方案2】:

您应该像这样将 form.type 标记添加到您的服务定义中:

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

【讨论】:

  • 未解决! symfony 唯一需要我的是将类从类:App\Bundle\mybundle\Form\ProfileKeyType 更改为类:Appmybundle\Form\ProfileKeyType 但错误是相同的:类型错误:参数 1 传递给 App\Bundle\myBundle\Form\ ProfileKeyType::__construct() 必须是 Doctrine\ORM\EntityManager 的实例,没有给出,在 D:\eclipse_neon_workspace\iSerPa\vendor\symfony\symfony\src\Symfony\Component\Form\FormRegistry.php 第 85 行调用
  • 你是如何创建表单的?
  • 使用 CRUD 实用程序,在生成的控制器 ProfileKeyController $profileKey = new Profilekey(); $form = $this->createForm('App\Bundle\myBundle\Form\ProfileKeyType', $profileKey); $form->handleRequest($request); .... return $this->render('profilekey/new.html.twig', array( 'profileKey' => $profileKey, 'form' => $form->createView(), )); }
  • 尝试在控制器中获取服务并查看 $this->get('serpabackend.profile_key'); 是否出现错误您还可以使用 bin/console |grep "serpabackend.profile_key" 检查您的服务是否正确加载
  • 省略了控制台命令的一部分。 bin/console debug:container serpabackend 是的,看起来服务似乎没有被创建。我想您也可以尝试清除缓存,但只要您处于开发模式就没有关系。
猜你喜欢
  • 1970-01-01
  • 2010-10-23
  • 2015-01-25
  • 1970-01-01
  • 1970-01-01
  • 2021-05-02
  • 1970-01-01
  • 2017-07-17
  • 2020-06-05
相关资源
最近更新 更多