Как внедрить репозиторий в сервис в Symfony?
мне нужно ввести два объекта в ImageService. Один из них является экземпляром Repository/ImageRepository, который я получаю такой:
$image_repository = $container->get('doctrine.odm.mongodb')
->getRepository('MycompanyMainBundle:Image');
Итак, как я могу заявить, что в моих услугах.в формате YML? Вот сервис:
namespace MycompanyMainBundleServiceImage;
use DoctrineODMMongoDBDocumentRepository;
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);
}
}
5 ответов:
вот очищенное решение для тех, кто приходит из Google, как я:
обновление: вот решение 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')
2017 и Symfony 3.3+ сделал это гораздо проще.
проверьте мой пост как использовать репозиторий с доктриной в качестве сервиса в Symfony для более общего описания.
в ваш код, все, что вам нужно сделать, это использовать композиция по наследству - один из твердых образцов.
1. Создайте собственный репозиторий без прямой зависимости от доктрины
<?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
# app/config/services.yml services: _defaults: autowire: true MycompanyMainBundle\: resource: ../../src/MycompanyMainBundle3. Теперь вы можете добавить любую зависимость в любом месте с помощью constructo injection
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 entity определяет службу, например, в моем случае для определения a (я использую 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')]);в любом случае у вас есть хороший тестируемый способ функционального тестирования вашей базы данных beavior также он помогает вам издеваться в случае, если вы хотите проверить свою службу без необходимости слишком беспокоиться о том, как это сделать. Например, давайте предположим, у нас есть следующий сервис:
//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 } }таким образом, вы можете легко издеваться/hardcode/эмулировать поведение метода
searchопределена вSomeRepositoryнасмехаясь над aither theRepositoryServiceAdapterв подходе без наследования илиContactEmailRepositoryServiceAdapterв наследство одна.
Comments