Inject the same service with different aliases in Symfony
<p>I’m not sure if what you’re asking is accurate, so let me explain<br /><br />I use foselasticabundance with this configuration:</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>To<span style="white-space:normal;">this way</span>query my article index:</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>But there is a mistake: </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>I changed it</p>
<pre class="brush:php;toolbar:false;"># api/config/services.yaml
services:
#...
FOSElasticaBundleFinderTransformedFinder:
alias: fos_elastica.finder.articles
public: true</pre>
<p>This is fine<br /><br />But now I want to add a second indicator, 0 percent</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>But now I don't know how to query fdocenttetes index as transformmedfinder has alias fos_elastica.finder.文章,所以如果我创建另一个服务fdocteteservice:</p>
<pre class="lang-php prettyprint-override"><code><?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);
}
}
</code></pre>
<p>那现在咋办</p>
<pre class="brush:php;toolbar:false;">$ 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</pre>
<pre class="brush:php;toolbar:false;">$ php bin/console debug:autowiring | grep fos_elastica.finder
FOSElasticaBundleFinderTransformedFinder (fos_elastica.finder.articles)</pre>
<p><br /></p>