Symfony 如何基于其他服务配置捆绑包?
我想根据可以从实体存储库获得的实体列表动态配置我的Elastica索引类型。要做到这一点,我必须有存储库的实例,这样条令EntityManager就必须存在,这样依赖注入容器就必须被编译。但当它被编译时,配置Elastica就太迟了,因为容器被冻结了 这就是我所尝试的:Symfony 如何基于其他服务配置捆绑包?,symfony,elastica,Symfony,Elastica,我想根据可以从实体存储库获得的实体列表动态配置我的Elastica索引类型。要做到这一点,我必须有存储库的实例,这样条令EntityManager就必须存在,这样依赖注入容器就必须被编译。但当它被编译时,配置Elastica就太迟了,因为容器被冻结了 这就是我所尝试的: #config.yml fos_elastica: clients: default: { host: 127.0.0.1, port: 9200 } indexes: app:
#config.yml
fos_elastica:
clients:
default: { host: 127.0.0.1, port: 9200 }
indexes:
app:
types: %elastic_types%
#application bundle
public function build(ContainerBuilder $container){
$types = array(
'tag' => array(
'mappings' => array(
'text' => array()
),
'persistence' => array(
'driver' => 'orm',
'model' => 'Cloud\AdmBundle\Entity\Tag',
'provider' => array(),
'listener' => array(),
'finder' => array()
)
)
);
$container->setParameter('elastic_types', $types);
}
它可以工作,但我没有能力在这里使用EntityRepository
#application bundle
public function setContainer(ContainerInterface $container = null) {
parent::setContainer($container);
$container->setParameter('elastic_types', array('anything param'));
}
现在设置任何参数都为时已晚,因为容器已编译且我得到了执行选项:
ParameterNotFoundException in ParameterBag.php line 106:
You have requested a non-existent parameter "elastic_types".
如何执行此操作?从构建函数中的容器获取entitymanager不起作用$em=$container->get'doctrine'->getManager;不起作用,因为$container是ContainerBuilder的实例,并且还没有初始化服务。如果我尝试得到错误:服务定义原则不存在。关于初始配置的全部内容是,它是一个静态过程,不会在不同的请求之间改变。容器实际上在缓存目录中作为一个大php文件结束。我认为您需要一种不同的方法。好的,但在配置初始化时只能执行一次,而不是针对每个请求。实体列表很少更改,但在ElasticSearch中自动更改映射可能会有些困难。您应该开始查看配置加载程序,这可能是您需要的