本文介绍了Symfony2/JmsDIExtraBundle使用注释将存储库注入服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我正在使用带有JMSDIExtraBundle的注释.我的问题是:如何告诉我的应用程序存储库应该是服务,所以我可以使用注释将其注入到另一个服务中.我只知道使用XML文件将存储库定义为服务.但这是一个非常缓慢的过程(将其与简单的@DI \ Service相比,我更喜欢在yml或xml文件中进行定义)./p>

我找到了XML的替代解决方案,但我认为这是个坏主意:

   ####CustomService.php####
   /**
     * @param EntityManager $em
     *
     * @DI\InjectParams({
     *     "em" = @DI\Inject("doctrine.orm.entity_manager")
     * })
     */
    public function __construct(EntityManager $em) {
        $this->em = $em;
    }

在服务中的某个地方:

$entityRepository = $this->em->getRepository(AcmeBundle:Entity);

有什么想法可以在需要使用存储库时如何加快编码速度吗?

解决方案

您可以将实体存储库定义为服务并将其注入服务中.

作为示例,您可以将仓库配置为以下服务:

以标准的Symfony服务定义方式:

   ####service.xml####
    <service id="acme.user.repository"
             class="Doctrine\ORM\EntityRepository"
             factory-service="doctrine.orm.entity_manager"
             factory-method="getRepository">
        <argument>AcmeDemoBundle:User</argument>
    </service>

或使用JMSDiExtraBundle中定义的factory to Service annotation(请参见):

/**
 * @Service("acme.user.repository", factoryService = "doctrine", factoryMethod="getRepository", factoryMethodArguments={
 * "persistentObjectName" = "Acme\DemoBundle\Entity\User"
 * } )
*/

然后注入并将其用作:

   ####CustomService.php####
   /**
     * @param Doctrine\ORM\EntityRepository $repo
     *
     * @DI\InjectParams({
     *     "repo" = @DI\Inject("acme.user.repository")
     * })
     */
    public function __construct(EntityRepository $repo) {
        $this->repo = $repo;
    }

不是很好的加速器,但是只允许注入您需要的东西

希望获得帮助

In my project I’m using annotations with JMSDIExtraBundle.My question is: how I can tell my app that repository should be service so, I can inject it in another service using annotations.Only way I know is using XML file to define repository as service. But it’s a very slow process(comparing it to simple @DI\Service which I prefer over defining it in yml or xml files).

I found an alternative solution to XML but I think it's bad idea:

   ####CustomService.php####
   /**
     * @param EntityManager $em
     *
     * @DI\InjectParams({
     *     "em" = @DI\Inject("doctrine.orm.entity_manager")
     * })
     */
    public function __construct(EntityManager $em) {
        $this->em = $em;
    }

And somewhere in service:

$entityRepository = $this->em->getRepository(AcmeBundle:Entity);

Any ideas how I can speed up coding process when I need to use repository?

解决方案

You can define your entity repository as a service and inject it into the service.

As Example, you can configure the repo as a service like:

In a standard Symfony Service definition Way:

   ####service.xml####
    <service id="acme.user.repository"
             class="Doctrine\ORM\EntityRepository"
             factory-service="doctrine.orm.entity_manager"
             factory-method="getRepository">
        <argument>AcmeDemoBundle:User</argument>
    </service>

Or using the factory to Service annotation defined in the JMSDiExtraBundle (see this):

/**
 * @Service("acme.user.repository", factoryService = "doctrine", factoryMethod="getRepository", factoryMethodArguments={
 * "persistentObjectName" = "Acme\DemoBundle\Entity\User"
 * } )
*/

And Inject and use it as:

   ####CustomService.php####
   /**
     * @param Doctrine\ORM\EntityRepository $repo
     *
     * @DI\InjectParams({
     *     "repo" = @DI\Inject("acme.user.repository")
     * })
     */
    public function __construct(EntityRepository $repo) {
        $this->repo = $repo;
    }

Is not a great speedup, but permit to only inject what you need

Hope this help

这篇关于Symfony2/JmsDIExtraBundle使用注释将存储库注入服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 22:37