4

我有一个捆绑包,它保存在私有 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 per class whose id is the fully-qualified class name
    CbmLtd\UvmsBundle\:
        resource: '../../vendor/cbmltd/uvms-bundle/*'
        exclude: '../../vendor/cbmltd/uvms-bundle/{Entity,Repository}'

    UvmsApiV1Bundle\:
        resource: '../../src/UvmsApiV1Bundle/*'
        exclude: '../../src/UvmsApiV1Bundle/{Entity,Repository}'

上面给出了以下例外:

无法自动装配服务“UvmsApiV1Bundle\Service\DealerService”:方法“__construct()”的参数“$repository”引用类“CbmLtd\UvmsBundle\Repository\DealerRepository”,但不存在此类服务。它不能自动注册,因为它来自不同的根命名空间。

好的,所以我想我需要将此存储库明确声明为服务。我在 Symfony 3.3 文档中找不到任何关于将存储库声明为服务的内容,并且 2.8 语法也不起作用。

我将此添加到我的 services.yml 中:

services:
    ...
    CbmLtd\UvmsBundle\DealerRepository:
        class: CbmLtd\UvmsBundle\Entity\DealerRepository
        factory_service: doctrine.orm.entity_manager
        factory_method: getRepository
        arguments:
            - CbmLtd\UvmsBundle\Entity\Dealer

但我仍然得到这个例外:

无法自动装配服务“UvmsApiV1Bundle\Service\DealerService”:方法“__construct()”的参数“$repository”引用类“CbmLtd\UvmsBundle\Repository\DealerRepository”,但不存在此类服务。它不能自动注册,因为它来自不同的根命名空间。

我无法对 CbmLtd\UvmsBundle 进行任何更改,因为它被多个应用程序使用。任何帮助,将不胜感激。我确实花了几个小时在这上面,这非常令人沮丧。

4

4 回答 4

5

我能够使用以下 services.yml 做到这一点:

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 per class whose id is the fully-qualified class name
    CbmLtd\UvmsBundle\:
        resource: '../../vendor/cbmltd/uvms-bundle/*'
        exclude: '../../vendor/cbmltd/uvms-bundle/{Entity,Repository}'

    CbmLtd\UvmsBundle\Repository\DealerRepository:
        factory: doctrine.orm.entity_manager:getRepository
        arguments:
            - CbmLtd\UvmsBundle\Entity\Dealer

    UvmsApiV1Bundle\:
        resource: '../../src/UvmsApiV1Bundle/*'
        exclude: '../../src/UvmsApiV1Bundle/{Entity,Repository}'

我不得不稍微改变我的控制器,但它现在可以工作了。

于 2017-07-02T10:25:34.450 回答
1

2018+ 和 Symfony 3.3+ 答案

您可以更进一步,将存储库用作服务

这样你就可以在服务配置中使用 Symfony 的自动装配和简单的配置。

1. 服务

services:
    _defaults:
        autowire: true
        autoconfigure: true

    CbmLtd\UvmsBundle\:
        resource: '../../vendor/cbmltd/uvms-bundle/*'
        exclude: '../../vendor/cbmltd/uvms-bundle/{Entity}'

    UvmsApiV1Bundle\:
        resource: '../../src/UvmsApiV1Bundle/*'
        exclude: '../../src/UvmsApiV1Bundle/{Entity}'

2. 存储库

namespace CbmLtd\UvmsBundle\Repository\DealerRepository;

final class DealerRepository
{
    /**
     * @var Doctrine\ORM\EntityRepository
     */
    private $repository;

    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->repository = $entityManager->getRepository(Dealer::class);
    }

    public function find($id)
    {
        return $this->repository->find($id);
    }
}
于 2018-05-18T19:52:52.110 回答
-1

您的服务声明中有错字,您忘记了“存储库”命名空间。此外,该类位于“实体”还是“存储库”中?服务 id 应该与实际的完全限定类名相同。在 services.yml 中,尝试:

CbmLtd\UvmsBundle\Repository\DealerRepository:
    class: CbmLtd\UvmsBundle\Repository\DealerRepository

代替:

CbmLtd\UvmsBundle\DealerRepository:
    class: CbmLtd\UvmsBundle\Entity\DealerRepository
于 2017-07-14T10:55:18.087 回答
-1

尝试更改您的存储库服务声明

your_bundle.your_entity_repository:
    class: Doctrine\ORM\EntityRepository
    factory: ['@doctrine.orm.default_entity_manager', getRepository]
    arguments:
        - YourBundle\Entity\YourEntity

然后通过像这样引用它来注入 repo:

your_bundle.your_service:
    class: YourBundle\Service\YourService
    arguments:
        - "@your_bundle.your_entity_repository"
    calls:
        - [setContainer, ['@service_container']]
于 2017-07-02T10:41:26.397 回答