我需要将两个对象注入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);
}
}
答案 0 :(得分:103)
这是一个清洁的解决方案,适用于像我一样来自谷歌的人:
更新:这是Symfony 2.6(及以上)解决方案:
services:
myrepository:
class: Doctrine\ORM\EntityRepository
factory: ["@doctrine.orm.entity_manager", getRepository]
arguments:
- MyBundle\Entity\MyClass
myservice:
class: MyBundle\Service\MyService
arguments:
- "@myrepository"
弃用解决方案(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"
答案 1 :(得分:45)
我找到了这个link,这对我有用:
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 :(得分:39)
如果不想将每个存储库定义为服务,从版本2.4
开始,您可以执行以下操作,(default
是实体管理器的名称):
@=service('doctrine.orm.default_entity_manager').getRepository('MycompanyMainBundle:Image')
答案 3 :(得分:7)
2017和Symfony 3.3 + 使这更加简单。在Symfony 4.x中也是如此。
查看我的帖子How to use Repository with Doctrine as Service in Symfony 以获得更详细的说明。
对于您的代码,您需要做的就是使用组合而不是继承 - 一种SOLID模式。
<?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();
}
}
# app/config/services.yml
services:
_defaults:
autowire: true
MycompanyMainBundle\:
resource: ../../src/MycompanyMainBundle
use MycompanyMainBundle\Repository\ImageRepository;
class ImageService
{
public function __construct(ImageRepository $imageRepository)
{
$this->imageRepository = $imageRepository;
}
}
答案 4 :(得分:0)
对于 Symfony 5 来说真的很简单,不需要 services.yml 来注入依赖:
private $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
$this->em->getRepository(ClassName::class)
将 ClassName 替换为您的实体名称。
答案 5 :(得分:-1)
在我的案例中,以@TomášVotruba的回答为基础,为此question我提出了以下方法:
创建通用适配器类:
namespace AppBundle\Services;
use Doctrine\ORM\EntityManagerInterface;
class RepositoryServiceAdapter
{
private $repository=null;
/**
* @param EntityManagerInterface the Doctrine entity Manager
* @param String $entityName The name of the entity that we will retrieve the repository
*/
public function __construct(EntityManagerInterface $entityManager,$entityName)
{
$this->repository=$entityManager->getRepository($entityName)
}
public function __call($name,$arguments)
{
if(empty($arrguments)){ //No arguments has been passed
$this->repository->$name();
} else {
//@todo: figure out how to pass the parameters
$this->repository->$name(...$argument);
}
}
}
然后为foreach实体定义服务,例如在我的情况下定义一个服务(我使用php定义symfony服务):
$container->register('ellakcy.db.contact_email',AppBundle\Services\Adapters\RepositoryServiceAdapter::class)
->serArguments([new Reference('doctrine'),AppBundle\Entity\ContactEmail::class]);
与上述步骤1相同
例如,扩展RepositoryServiceAdapter
类:
namespace AppBundle\Service\Adapters;
use Doctrine\ORM\EntityManagerInterface;
use AppBundle\Entity\ContactEmail;
class ContactEmailRepositoryServiceAdapter extends RepositoryServiceAdapter
{
public function __construct(EntityManagerInterface $entityManager)
{
parent::__construct($entityManager,ContactEmail::class);
}
}
注册服务:
$container->register('ellakcy.db.contact_email',AppBundle\Services\Adapters\RepositoryServiceAdapter::class)
->serArguments([new Reference('doctrine')]);
在两种情况下,您都有一种很好的可测试方法来对数据库性能进行功能测试,它还可以帮助您进行模拟,以防您要对服务进行单元测试而无需过多地担心如何做。例如,假设我们具有以下服务:
//Namespace definitions etc etc
class MyDummyService
{
public function __construct(RepositoryServiceAdapter $adapter)
{
//Do stuff
}
}
RepositoryServiceAdapter会适配以下存储库:
//Namespace definitions etc etc
class SomeRepository extends \Doctrine\ORM\EntityRepository
{
public function search($params)
{
//Search Logic
}
}
因此,您可以通过模拟非继承方法中的search
或SomeRepository
来轻松地对RepositoryServiceAdapter
中定义的方法ContactEmailRepositoryServiceAdapter
的行为进行模拟/硬编码/模拟。在继承中。
或者,您可以定义以下工厂:
namespace AppBundle\ServiceFactories;
use Doctrine\ORM\EntityManagerInterface;
class RepositoryFactory
{
/**
* @param EntityManagerInterface $entityManager The doctrine entity Manager
* @param String $entityName The name of the entity
* @return Class
*/
public static function repositoryAsAService(EntityManagerInterface $entityManager,$entityName)
{
return $entityManager->getRepository($entityName);
}
}
然后通过执行以下操作切换到php服务注释:
将其放置到文件./app/config/services.php
中(对于symfony v3.4,假定.
是对象的根目录)
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
$definition = new Definition();
$definition->setAutowired(true)->setAutoconfigured(true)->setPublic(false);
// $this is a reference to the current loader
$this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', '../../src/AppBundle/{Entity,Repository,Tests,Interfaces,Services/Adapters/RepositoryServiceAdapter.php}');
$definition->addTag('controller.service_arguments');
$this->registerClasses($definition, 'AppBundle\\Controller\\', '../../src/AppBundle/Controller/*');
然后摇动./app/config/config.yml
(假设.
是您个人的根源)
imports:
- { resource: parameters.yml }
- { resource: security.yml }
#Replace services.yml to services.php
- { resource: services.php }
#Other Configuration
然后,您可以按以下方式使用该服务(在我使用名为Item
的虚拟实体的示例中使用):
$container->register(ItemRepository::class,ItemRepository::class)
->setFactory([new Reference(RepositoryFactory::class),'repositoryAsAService'])
->setArguments(['$entityManager'=>new Reference('doctrine.orm.entity_manager'),'$entityName'=>Item::class]);
作为通用提示,切换到php
服务注释还可以使您轻松进行上面所述的更高级的服务配置。对于代码段,请使用我使用factory
方法制作的特殊repository。