Symfony 2:从存储库创建服务 [英] Symfony 2: Creating a service from a Repository

查看:112
本文介绍了Symfony 2:从存储库创建服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Symfony,我一直在尝试使用存储库创建一个服务。
我已经从generate:entity创建了我的存储库和实体,所以它们应该是正常的。



到目前为止,我在services.yml中有:

 参数:
mytest.entity:TestTestBundle:品牌
mytest.class:Test\TestBundle \\Entity\Brand
default_repository.class:Doctrine\ORM\EntityRepository

services:
myservice:
class:%default_repository.class%
工厂服务:doctrine.orm.default_entity_manager
工厂方法:getRepository
参数:
- %mytest.entity%

但是当我尝试调用该服务时,我会收到以下错误:

  Catchable致命错误:参数2传递给Doctrine\ORM\EntityRepository :: __ construct()必须是Doctrine\ORM\Mapping\ClassMetadata的实例,没有给出,

然后我tr用于创建仅使用实体的服务。我的services.yml将如下所示:

 服务:
myservice:
class:%mytest.class %
工厂服务:doctrine.orm.default_entity_manager
工厂方法:getRepository
参数:
- %mytest.entity%

但是为此,我得到:

 错误:调用未定义的方法
Test\TestBundle\Entity\Brand :: findAll

有没有人知道我做错了什么?



谢谢

解决方案

这是我们如何在KnpRadBundle中进行的: https://github.com/ KnpLabs / KnpRadBundle / blob / development / DependencyInjection / Definition / DoctrineRepositoryFactory.php#L9



最后应该是:

 我的_service:
class:Doctrine\Common\Persistence\ObjectRepository
factory_service:doctrine#这是一个Registry
factory_method的实例:getRepository
arguments:[%mytest.entity %]

更新



自2.4以来,教义允许覆盖默认的存储库工厂。



这是在symfony中实现它的一种可行方法:https://gist.github.com/docteurklein/9778800


I'm learning Symfony and I've been trying to create a service, using a repository. I've created my repositories and entities from generate:entity, so they should be fine.

So far what I got in my services.yml is:

parameters:
    mytest.entity: TestTestBundle:Brand
    mytest.class:  Test\TestBundle\Entity\Brand
    default_repository.class: Doctrine\ORM\EntityRepository

services:
     myservice:
          class: %default_repository.class%
          factory-service: doctrine.orm.default_entity_manager
          factory-method: getRepository
          arguments:
            - %mytest.entity%

But when I try to call the service, I get this error:

Catchable Fatal Error: Argument 2 passed to Doctrine\ORM\EntityRepository::__construct() must be an instance of Doctrine\ORM\Mapping\ClassMetadata, none given, called in 

Then I tried to create the service just using an entity. My services.yml would look like:

services:
     myservice:
          class: %mytest.class%
          factory-service: doctrine.orm.default_entity_manager
          factory-method: getRepository
          arguments:
            - %mytest.entity%

But for this, I get:

Error: Call to undefined method 
                Test\TestBundle\Entity\Brand::findAll

Does anybody know what am I doing wrong?

Thanks

解决方案

Here is how we did it in KnpRadBundle: https://github.com/KnpLabs/KnpRadBundle/blob/develop/DependencyInjection/Definition/DoctrineRepositoryFactory.php#L9

Finally it should be:

my_service:
    class: Doctrine\Common\Persistence\ObjectRepository
    factory_service: doctrine # this is an instance of Registry
    factory_method: getRepository
    arguments: [ %mytest.entity% ]

UPDATE

Since 2.4, doctrine allows to override the default repositor factory.

Here is a possible way to implement it in symfony: https://gist.github.com/docteurklein/9778800

这篇关于Symfony 2:从存储库创建服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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