如何将资源库注入Symfony2中的服务?

我需要注入两个对象到ImageService 。 其中之一是Repository/ImageRepository一个实例,我得到这样的:

 $image_repository = $container->get('doctrine.odm.mongodb') ->getRepository('MycompanyMainBundle:Image'); 

那么如何在我的services.yml中声明呢? 这是服务:

 namespace Mycompany\MainBundle\Service\Image; use Doctrine\ODM\MongoDB\DocumentRepository; class ImageManager { private $manipulator; private $repository; public function __construct(ImageManipulatorInterface $manipulator, DocumentRepository $repository) { $this->manipulator = $manipulator; $this->repository = $repository; } public function findAll() { return $this->repository->findAll(); } public function createThumbnail(ImageInterface $image) { return $this->manipulator->resize($image->source(), 300, 200); } } 

这里是一个像我这样来自谷歌的清理解决scheme:

更新:这里是Symfony 2.6(及更高版本)解决scheme:

 services: myrepository: class: Doctrine\ORM\EntityRepository factory: ["@doctrine.orm.entity_manager", getRepository] arguments: - MyBundle\Entity\MyClass myservice: class: MyBundle\Service\MyService arguments: - "@myrepository" 

弃用的解决scheme(Symfony 2.5及更低版本):

 services: myrepository: class: Doctrine\ORM\EntityRepository factory_service: doctrine.orm.entity_manager factory_method: getRepository arguments: - MyBundle\Entity\MyClass myservice: class: MyBundle\Service\MyService arguments: - "@myrepository" 

我发现这个链接 ,这对我工作:

 parameters: image_repository.class: Mycompany\MainBundle\Repository\ImageRepository image_repository.factory_argument: 'MycompanyMainBundle:Image' image_manager.class: Mycompany\MainBundle\Service\Image\ImageManager image_manipulator.class: Mycompany\MainBundle\Service\Image\ImageManipulator services: image_manager: class: %image_manager.class% arguments: - @image_manipulator - @image_repository image_repository: class: %image_repository.class% factory_service: doctrine.odm.mongodb factory_method: getRepository arguments: - %image_repository.factory_argument% image_manipulator: class: %image_manipulator.class% 

如果不想将每个存储库定义为服务,从版本2.4开始,您可以执行以下操作( default是实体pipe理器的名称):

 @=service('doctrine.orm.default_entity_manager').getRepository('MycompanyMainBundle:Image') 

2017和Symfony 3.3+使这更简单。

检查我的post如何在Symfony中使用Repository与Doctrine作为服务来获得更为一般的描述。

对于你的代码,你所需要做的就是使用inheritance的组合 – 一种SOLID模式。

1.创build自己的存储库,而不直接依赖于Doctrine

 <?php namespace MycompanyMainBundle\Repository; use Doctrine\ORM\EntityManagerInterface; use MycompanyMainBundle\Entity\Image; class ImageRepository { private $repository; public function __construct(EntityManagerInterface $entityManager) { $this->repository = $entityManager->getRepository(Image::class); } // add desired methods here public function findAll() { return $this->repository->findAll(); } } 

2.使用基于PSR-4的自动注​​册添加configuration注册

 # app/config/services.yml services: _defaults: autowire: true MycompanyMainBundle\: resource: ../../src/MycompanyMainBundle 

3.现在你可以通过构造注入来添加任何依赖

 use MycompanyMainBundle\Repository\ImageRepository; class ImageService { public function __construct(ImageRepository $imageRepository) { $this->imageRepository = $imageRepository; } }