我不确定问的是否准确,那我解释一下
我用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)
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
这是我从现有项目的解决方案。
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。
然后你必须克隆这些服务。记不清了,太久了搞忘了