如何将实体管理器添加到自定义类或服务中? [英] How can I add the Entity Manager to a custom class or service?
问题描述
在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屋!