在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 "__construct()" references class "FOSElasticaBundleFinderTransformedFinder" but no such service exists. You should maybe alias this class to the existing "fos_elastica.finder.articles" service.</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>