7

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'

1 Answer 1

22

I think you need

$root
    ->children()
        ->arrayNode('entities')
        ->addDefaultsIfNotSet()
        ->prototype('scalar')->end()
        ->defaultValue(array(
            'Acme\BaseBundle\Entity\DefaultEntity1',
            'Acme\BaseBundle\Entity\DefaultEntity2',
        ))
    ->end()
Sign up to request clarification or add additional context in comments.

2 Comments

I just figured out somewhere else about the prototype method. But you need to add ->end() like ->prototype('scalar')->end() or else the ->defaultValue( part will be applied to each array item.
And thanks for telling me about ->addDefaultsIfNotSet(). I was in the middle of working that one out just now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.