在Symfony中用不同别名注入相同的服务
P粉662802882
P粉662802882 2023-08-07 21:55:08
[PHP讨论组]

我不确定问的是否准确,那我解释一下

我用foselasticabundance与此配置:


# Read the documentation: https://github.com/FriendsOfSymfony/FOSElasticaBundle/blob/master/doc/setup.md
fos_elastica:
    clients:
        default: { url: '%env(ELASTICSEARCH_URL)%' }
    indexes:
        articles:
            index_name: 'articles'
            persistence:
                # the driver can be orm, mongodb or phpcr
                driver: orm
                model: AppEntitySiteFArticleFArticleProp
                provider: ~
                finder: ~

这样查询我的文章索引:

<?php

namespace AppService;

use FOSElasticaBundleFinderTransformedFinder;

class FArticleService
{
    public function __construct(
        private readonly TransformedFinder $finder,
    )
    {
    }

    public function query(array $query, int $page, int $size)
    {
        $results = $this->finder->createRawPaginatorAdapter($query)->getResults(($page * $size) - $size, $size);
    }
}

但是有个错:

In DefinitionErrorExceptionPass.php line 51:
                                                                                                                                                                                                                                                                                            
  Cannot autowire service "AppServiceFArticleService": argument "$finder" of method "__construct()" references class "FOSElasticaBundleFinderTransformedFinder" but no such service exists. You should maybe alias this class to the existing "fos_elastica.finder.articles" service.

我改了一下

# api/config/services.yaml
services:
  #...
  FOSElasticaBundleFinderTransformedFinder:
    alias: fos_elastica.finder.articles
    public: true

这样还挺好

但现在我要增加第二个指标,0百分

# Read the documentation: https://github.com/FriendsOfSymfony/FOSElasticaBundle/blob/master/doc/setup.md
fos_elastica:
  clients:
    default: { url: '%env(ELASTICSEARCH_URL)%' }
  indexes:
    articles:
      index_name: 'articles'
      persistence:
        # the driver can be orm, mongodb or phpcr
        driver: orm
        model: AppEntitySiteFArticleFArticleProp
        provider: ~
        finder: ~
    fdocentetes:
      index_name: 'fdocentetes'
      persistence:
        # the driver can be orm, mongodb or phpcr
        driver: orm
        model: AppEntitySageFDocentete
        provider: ~
        finder: ~

但现在我不知道如何查询fdocenttetes索引作为transformmedfinder有别名fos_elastica.finder。文章,所以如果我创建另一个服务fdocteteservice:

<?php

namespace AppService;

use FOSElasticaBundleFinderTransformedFinder;

class FDocenteteService
{
    public function __construct(
        private readonly TransformedFinder $finder,
    )
    {
    }

    public function query(array $query, int $page, int $size)
    {
        // this will query on the index articles while I want to query on index fdocentetes
        $results = $this->finder->createRawPaginatorAdapter($query)->getResults(($page * $size) - $size, $size);
    }
}

那现在咋办

$ php bin/console debug:container | grep fos_elastica.finder
  FOSElasticaBundleFinderTransformedFinder      alias for "fos_elastica.finder.articles"
  fos_elastica.finder                              FOSElasticaBundleFinderTransformedFinder
  fos_elastica.finder.articles                     FOSElasticaBundleFinderTransformedFinder
  fos_elastica.finder.fdocentetes                  FOSElasticaBundleFinderTransformedFinder
$ php bin/console debug:autowiring | grep fos_elastica.finder
 FOSElasticaBundleFinderTransformedFinder (fos_elastica.finder.articles)


P粉662802882
P粉662802882

全部回复(1)
P粉777458787

这是我从现有项目的解决方案。

app.service.configuration_service_foo:
    public: true
    class: App\Service\ConfigurationService
    arguments:
      - '@app.config.foo_config'

app.service.configuration_service_bar:
    public: true
    class: App\Service\ConfigurationService
    arguments:
      - '@app.config.bar_config'
private ConfigurationService $configServiceFoo;
private ConfigurationService $configServiceBar;

public function __construct(
    ContainerInterface $container,
) {
    $this->configServiceFoo = clone $container->get('app.service.configuration_service_foo');
    $this->configServiceBar = clone $container->get('app.service.configuration_service_bar');
}

也应该还有别的方法。

不可能通过自动调用两次注入同一个对象。所以,注入了ContainerInterface。

然后你必须克隆这些服务。记不清了,太久了搞忘了


热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号