我正在学习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/