相关疑难解决方法(0)

如何将存储库注入Symfony中的服务?

我需要注入两个对象ImageService.其中一个是Repository/ImageRepository我的例子:

$image_repository = $container->get('doctrine.odm.mongodb')
    ->getRepository('MycompanyMainBundle:Image');
Run Code Online (Sandbox Code Playgroud)

那么如何在我的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);
    }
}
Run Code Online (Sandbox Code Playgroud)

php dependency-injection symfony

75
推荐指数
4
解决办法
5万
查看次数

Symfony 3.3将存储库注入服务

我有一个捆绑包,它存放在一个私有的Satis存储库中,因为它的实体和存储库在多个应用程序之间共享.

使用该捆绑包的其余应用程序是Symfony 2.7和2.8应用程序.我正在开发一个新的应用程序,要求是使用Symfony 3.3.

在symfony 3.3应用程序中,我在我的services.yml中尝试了这个:

# Learn more about services, parameters and containers at
# http://symfony.com/doc/current/service_container.html
parameters:
    #parameter_name: value

services:
# default configuration for services in *this* file
    _defaults:
        autowire: true
        autoconfigure: true
        # this means you cannot fetch services directly from the container via $container->get()
        # if you need to do this, you can override this setting on individual services
        public: false

    # makes classes in src/AppBundle available to be used as services
    # this creates a service …
Run Code Online (Sandbox Code Playgroud)

doctrine dependency-injection symfony symfony-3.3

4
推荐指数
1
解决办法
7454
查看次数