1

I want to configure my index types of Elastica dynamically based on entity list which I can get from entity repository. To do this I must have instance of repository so doctrine EntityManager must exist so Depedency Inject container must by compiled. But when it is compiled is too late for configure Elastica because container is frozen.

This is what i tried:

#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);
}

It works but i don't have capabilities to use EntityRepository here.

#application bundle

public function setContainer(ContainerInterface $container = null) {
    parent::setContainer($container);
    $container->setParameter('elastic_types', array('anything param'));
}

Here is too late to set any params because container is compiled and i get execption:

ParameterNotFoundException in ParameterBag.php line 106:
You have requested a non-existent parameter "elastic_types".

How can i do this ?

  • getting the entitymanager from the container in the build function doesnt work? $em = $container->get('doctrine')->getManager(); – Nawfal Serrar Mar 24 '15 at 10:25
  • not work, because $container is instance of ContainerBuilder and don't have initialized services yet. If I try get error: The service definition "doctrine" does not exist. – Bartek Nowak Mar 24 '15 at 10:29
  • The whole thing about initial configuration is that it's a static process which does not change from request to request. The container actually ends up as a big php file in the cache directory. I think you will need a different approach. – Cerad Mar 24 '15 at 10:32
  • ok but it can be done only once when config is initialize, not for every request. List of entities is rarely changed but it would be awsome to automatically change mappings in ElasticSearch – Bartek Nowak Mar 24 '15 at 10:39
  • You should start looking at config loaders , this may be what you need http://symfony.com/fr/doc/current/components/dependency_injection/compilation.html – Nawfal Serrar Mar 24 '15 at 10:43
  • Have you tried a compiler pass? http://symfony.com/doc/current/cookbook/service_container/compiler_passes.html – qooplmao Mar 24 '15 at 13:41

0 Answers0