在Symfony中用不同别名注入相同的服务
P粉662802882
2023-08-07 21:55:08
[PHP讨论组]
<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"><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>
这是我从现有项目的解决方案。
也应该还有别的方法。
不可能通过自动调用两次注入同一个对象。所以,注入了ContainerInterface。
然后你必须克隆这些服务。记不清了,太久了搞忘了