如何将实体管理器添加到自定义类或服务中? [英] How can I add the Entity Manager to a custom class or service?

查看:100
本文介绍了如何将实体管理器添加到自定义类或服务中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在Symfony2中,我如何将Doctrine的实体管理器添加到自定义类或服务中? $ em = $ this-> get(doctrine.orm.entity_manager);
$ em = $ this-> getDoctrine() - > getEntityManager() ;



两个都失败了,这导致我尝试用我的自定义类/服务扩展Controller类,并且死于一个巨大的球

解决方案

您需要将实体管理器服务注入到您的自定义服务中。您的服务定义应如下所示:

  my.service.name:
class:my\class
参数:[@ doctrine.orm.default_entity_manager]

确保您的服务的 __构造方法将实体管理器作为参数。



请参阅服务容器一章了解更多信息。



BTW, $ this-> getDoctrine()是一种快捷方式,只能在扩展的类中运行Symfony\Bundle\FrameworkBundle\Controller\Controller


In Symfony2, how can I go about adding Doctrine's entity manager to a custom class or service?

I have tried $em = $this->get("doctrine.orm.entity_manager"); and $em = $this->getDoctrine()->getEntityManager();

Both failed, which led me to try and extend the Controller class with my custom class/service, and that died in a giant ball of fire.

解决方案

You need to inject the entity manager service into your custom service. Your service definition should look like this:

my.service.name:
  class:     my\class
  arguments: [ @doctrine.orm.default_entity_manager ]

Make sure that your service's __construct method takes the entity manager as an argument.

See the Service Container chapter for more info.

BTW, $this->getDoctrine() is a shortcut method that will only work in a class that extends Symfony\Bundle\FrameworkBundle\Controller\Controller

这篇关于如何将实体管理器添加到自定义类或服务中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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