Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the Symfony2 configuration class, how do I define an array node whose children don't have keys?

Using the configuration class, how do I define an array node without numeric keys? The children of the array do not represent further configuration options. Rather, they will be a list that will not be able to be overwritten selectively, only as a whole.

So far I have:

public function getConfigTreeBuilder()
{
    $treeBuilder = new TreeBuilder;
    $root = $treeBuilder->root('acme_base');

    $root
        ->children()
            ->arrayNode('entities')

                // Unfortunately, this doesn't work
                ->defaultValue(array(
                    'Acme\BaseBundle\Entity\DefaultEntity1',
                    'Acme\BaseBundle\Entity\DefaultEntity2',
                ))

            ->end()
        ->end();

    return $treeBuilder;
}

In app/config.yml, I want to be able to overwrite it like this:

acme_base:
  entities:
    - 'Acme\BaseBundle\Entity\AnotherEntity1'
    - 'Acme\BaseBundle\Entity\AnotherEntity2'
    - 'Acme\BaseBundle\Entity\AnotherEntity3'
    - 'Acme\BaseBundle\Entity\AnotherEntity4'
like image 246
mattalxndr Avatar asked Aug 13 '12 15:08

mattalxndr


1 Answers

I think you need

$root
    ->children()
        ->arrayNode('entities')
        ->addDefaultsIfNotSet()
        ->prototype('scalar')->end()
        ->defaultValue(array(
            'Acme\BaseBundle\Entity\DefaultEntity1',
            'Acme\BaseBundle\Entity\DefaultEntity2',
        ))
    ->end()
like image 118
solarc Avatar answered Nov 18 '22 18:11

solarc