[英]How to inject a repository into a service in Symfony?
我需要將兩個對象注入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);
}
}
對於像我這樣來自谷歌的人來說,這是一個清理過的解決方案:
更新:這是 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"
我找到了這個鏈接,這對我有用:
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
為實體管理器的名稱):
@=service('doctrine.orm.default_entity_manager').getRepository('MycompanyMainBundle:Image')
查看我的文章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;
}
}
就我而言,基於@Tomáš Votruba 的回答和這個問題,我提出了以下方法:
創建一個通用的適配器類:
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
}
}
因此,您可以通過模擬非繼承方法中的RepositoryServiceAdapter
或繼承方法中的ContactEmailRepositoryServiceAdapter
來輕松模擬/硬編碼/模擬SomeRepository
定義的方法search
的行為。
或者,您可以定義以下工廠:
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
(為symfony1.2 V3.4, .
假設你ptoject根)
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
( .
假定您的 ptoject 的根)
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
方法制作的特殊存儲庫。
對於 Symfony 5,它真的很簡單,不需要 services.yml 來注入依賴:
private $em; public function __construct(EntityManagerInterface $em) { $this->em = $em; }
$this->em->getRepository(ClassName::class)
通過將 ClassName 替換為您的實體名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.