jagemue
jagemue

Reputation: 393

Create multidimensional array with symfony configuration tree builder

I want to create a configuration for my own bundle like this:

my_filters:
    filter_type_a:
        - \MyBundle\My\ClassA
        - \MyBundle\My\ClassA2
    filter_type_b:
        - \MyBundle\My\ClassB

The my_filters should be an array of variable length, and the my_filters.filter_type_a should be an array of variable length,too.

I tried

$treeBuilder = new TreeBuilder(); 
$rootNode = $treeBuilder->root('my_bundle');
$rootNode
     ->children()
         ->arrayNode('my_filters')
             ->prototype('array')
                 ->prototype('array')
                     ->children()
                         ->scalarNode('my_filters')->end()
                      ->end()
                 ->end()
             ->end()
         ->end()
     ->end()

but i got the following error: Invalid type for path "my_bundle.my_filters.filter_type_a.0". Expected array, but got string.

Here is, where i set the configuration:

class MyBundleExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $this->loadServices($container);
        $this->loadConfig($configs, $container);
    }

    private function loadConfig(array $configs, ContainerBuilder $container)
    {       
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);
        $container->setParameter('my_bundle.my_filters', $config['my_filters']);
    }

    private function loadServices(ContainerBuilder $container)
    {
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
    }
}

I can not see my mistake, can anyone tell me?

Upvotes: 3

Views: 2951

Answers (1)

Michael Sivolobov
Michael Sivolobov

Reputation: 13240

To match config

my_bundle:
    my_filters:
        filter_type_a:
            - \MyBundle\My\ClassA
            - \MyBundle\My\ClassA2
        filter_type_b:
            - \MyBundle\My\ClassB

You need next code in config tree builder:

$rootNode = $treeBuilder->root('my_bundle');
$rootNode
     ->children()
         ->arrayNode('my_filters')
             ->prototype('array')
                 ->prototype('scalar')->end()
             ->end()
         ->end()
     ->end()

Upvotes: 4

Related Questions