我需要把两个物体注入ImageService.其中一个是Repository/ImageRepository的例子,我得到如下结果:

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

那么,我如何在我的服务中声明这一点呢.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);
    }
}

推荐答案

我找到了这个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%

Php相关问答推荐

如何在WooCommerce HPOS订单列表中创建自定义过滤器?

PHP Perl正则表达式—URL前面没有等号和可能的单引号或双引号

有条件的运费率基于购物车小计与WooPayments货币转换

如果再次调用SESSION_START(),会话.gc-max是否会重新启动?或者它是从第一次创建会话开始计算的?

WooCommerce购物车中仅允许一个可下载产品

仅在WooCommerce管理订单视图上显示订单项自定义元数据

从WooCommerce购物车和 checkout 页面自定义发货标签

在WooCommerce产品中按类型对产品属性术语进行分类以供显示

如何从Laravel中的Spatie\Dns\Records\A对象中提取IP地址

如何使用索引加速和优化MySQL搜索

如何在UML类图中可视化变量引用数组

在WooCommerce中只允许高价产品的BACS付款

使用DOMPDF获取PDF格式的本 map 像

如何解决 Laravel 外键错误?

AWS S3:当对象名称包含 % 时复制对象失败并出现错误 - 无效的复制源编码

加载 Xdebug PHP 7.4.0 失败

Laravel 发送邮箱问题 Swift_TransportException

自定义主键值未正确显示

php如何删除括号内的百分比值

如何获取用户之间的最新消息列表