Str*_*rnm 17 service repository symfony doctrine-orm
我正在学习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%
Run Code Online (Sandbox Code Playgroud)
但是当我尝试调用该服务时,我收到此错误:
Catchable Fatal Error: Argument 2 passed to Doctrine\ORM\EntityRepository::__construct() must be an instance of Doctrine\ORM\Mapping\ClassMetadata, none given, called in
Run Code Online (Sandbox Code Playgroud)
然后我尝试使用实体创建服务.我的services.yml看起来像:
services:
myservice:
class: %mytest.class%
factory-service: doctrine.orm.default_entity_manager
factory-method: getRepository
arguments:
- %mytest.entity%
Run Code Online (Sandbox Code Playgroud)
但为此,我得到:
Error: Call to undefined method
Test\TestBundle\Entity\Brand::findAll
Run Code Online (Sandbox Code Playgroud)
有人知道我做错了什么吗?
谢谢
Fra*_*ula 44
弃权警告:不再factory_service和factory_method.这是你应该怎么做的,因为Symfony 2.6(下面的Symfony 3.3+检查):
parameters:
entity.my_entity: "AppBundle:MyEntity"
services:
my_entity_repository:
class: AppBundle\Repository\MyEntityRepository
factory: ["@doctrine", getRepository]
arguments:
- %entity.my_entity%
Run Code Online (Sandbox Code Playgroud)
新的setFactory()方法是在Symfony 2.6中引入的.有关2.6之前的工厂的语法,请参阅旧版本.
http://symfony.com/doc/2.7/service_container/factories.html
编辑:看起来他们一直在改变这个,所以从Symfony 3.3开始就有了新的语法:
# app/config/services.yml
services:
# ...
AppBundle\Email\NewsletterManager:
# call the static method
factory: ['AppBundle\Email\NewsletterManagerStaticFactory', createNewsletterManager]
Run Code Online (Sandbox Code Playgroud)
请查看:http://symfony.com/doc/3.3/service_container/factories.html
Flo*_*ian 24
以下是我们在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% ]
Run Code Online (Sandbox Code Playgroud)
UPDATE
从2.4开始,doctrine允许覆盖默认的repositor工厂.
以下是在symfony中实现它的可能方法:https://gist.github.com/docteurklein/9778800
| 归档时间: |
|
| 查看次数: |
24841 次 |
| 最近记录: |