symfony - Symfony 2:从存储库创建服务

标签 symfony service doctrine-orm repository

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

到目前为止,我在services.yml中得到的是:

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%


但是,当我尝试调用该服务时,出现此错误:

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


然后,我尝试仅使用实体来创建服务。我的services.yml看起来像:

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


但是为此,我得到:

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


有人知道我在做什么错吗?

谢谢

最佳答案

这是我们在KnpRadBundle中执行的操作:https://github.com/KnpLabs/KnpRadBundle/blob/develop/DependencyInjection/Definition/DoctrineRepositoryFactory.php#L9

最后应该是:

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


更新

从2.4开始,主义允许覆盖默认的存储库工厂。

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

关于symfony - Symfony 2:从存储库创建服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17228417/

相关文章:

symfony - 独立 Symfony2 包中的功能测试

.htaccess - symfony2重写规则.htaccess app.php

mysql - Doctrine query builder 在 where 中添加条件

安卓在后台播放音乐

php - 带有别名的 SUM() 字段上的 Doctrine orderBy

php - Doctrine 说我应该在显式字段中拆分参数,但我的 ID 为空

php - 如何在不修改vendor的情况下自定义sylius_taxon_image_widget?

Symfony2 : phpinfo() using a twig template for layout?

service - 在不同环境的Service Bus Console中导入OSB Config Jar

php - 如何在 symfony2 全局辅助函数(服务)中访问服务容器?