搜尋

首頁  >  問答  >  主體

在Symfony中以不同別名註入相同的服務

<p>我不確定問的是否準確,那我解釋<br /><br />我用foselasticabundance與此配置:</p><p><br /> </p> <pre class="brush:php;toolbar:false;"># 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: ~</pre> <p>要<span style="white-space:normal;">這樣</span>查詢我的文章索引:</p> <pre class="brush:php;toolbar:false;"><?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); } }</pre> <p>但是有個錯:</p> <pre class="brush:php;toolbar:false;">In DefinitionErrorExceptionPass.php line 51: Cannot autowire service "AppServiceFArticleService": argument "$finder" of method "AppServiceFArticleService": argument "$finder" of method "__construct()" references class "FOSElasticaBundleFinderTransformedFinder" but no such service exists. You should maybe alias service class to the existing "fessionals. /pre> <p>我改了一下</p> <pre class="brush:php;toolbar:false;"># api/config/services.yaml services: #... FOSElasticaBundleFinderTransformedFinder: alias: fos_elastica.finder.articles public: true</pre> <p>這樣還蠻好<br /><br />但現在我要增加第二個指標,0百分</p><p><code></code> ;</p> <pre class="lang-yaml prettyprint-override"><code># 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: ~ </code></pre> <p>但現在我不知道如何查詢fdocenttetes索引作為transformmedfinder有別名fos_elastica.finder。文章,所以如果我建立另一個服務 fdocteteservice:</p> <pre class="lang-php Prettyprint-override"><代碼><?php 命名空間AppService; 使用 FOSElasticaBundleFinderTransformedFinder; FDocenteteService 類別 { 公共函數 __construct( 私有唯讀 TransformedFinder $finder, ) { } 公用函數查詢(陣列$query,int $page,int $size) { // 這將查詢索引文章,而我想查詢索引 fdocentetes $results = $this->finder->createRawPaginatorAdapter($query)->getResults(($page * $size) - $size, $size); } } </代號></pre> <p>那現在咋辦</p>
$ php bin/console debug:container | grep fos_elastica.finder
  「fos_elastica.finder.articles」的 FOSElasticaBundleFinderTransformedFinder 別名
  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><br />></p>
P粉662802882P粉662802882572 天前427

全部回覆(1)我來回復

  • P粉777458787

    P粉7774587872023-08-08 18:16:46

    這是我從現有專案的解決方案。

    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。

    然後你必須複製這些服務。記不清了,太久了搞忘了


    回覆
    0
  • 取消回覆