Phill Pafford
Phill Pafford

Reputation: 85378

Symfony2 yaml array options

In app/config/config.yml I've added some custom settings for my bundle

acme:
    acme_services:    
      service_a:
        options: { name: I, id: X, type: F, error: E }
      service_b:
        options: { name: J, id: Z, type: F, error: E }

Now in src/ACME/Bundle/ACMEBundle/DependencyInjection/Configuration.php how do I set the default and / or check for service_a/service_b?

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

    $rootNode
        ->children()
            // also removed the ->end() for each arrayNode but then I get a Fatal Error 
            ->arrayNode('acme_services')->end()
            ->arrayNode('another')->end()
            ->arrayNode('more')->end()
            ->arrayNode('blah')->end()
        ->end();

    return $treeBuilder;
}

So I need pull the service_a and service_b arrays but I'm getting Unrecognized options error for service_a and service_b.

The desired result is I would like to have both service_a and service_b in the acme_services array, this why I can validate against the acme_services array for whatever service is used, either service_a or service_b.

Note: In PHP I would write it like this: (not sure if this is correct but it's an example)

$acme_services = array(
    'acme_services' =>
        'service_a' => array(
            'options' => array(
                'name' => 'I',
                'id'   => 'X',
                'type' => 'F',
                'error'=> 'E',
            )
        ),
        'service_b' => array(
            'options' => array(
                'name' => 'J',
                'id'   => 'Z',
                'type' => 'F',
                'error'=> 'E',
            )
        )
);

Upvotes: 1

Views: 1886

Answers (3)

Henry
Henry

Reputation: 7891

Try this:

$rootNode
   ->children()
        ->arrayNode('acme_services')
            ->prototype('array')
                ->children()
                    ->arrayNode('options')
                        ->children()
                            ->scalarNode('name')->end()
                            ->scalarNode('id')->end()
                            ->scalarNode('type')->end()
                            ->scalarNode('error')->end()
                        ->end()
                    ->end()
                ->end()
            ->end()
        ->end()
    ->end()
->end();

Upvotes: 0

brensch
brensch

Reputation: 106

What you want to use is Prototypes, something like:

$rootNode
    ->children()
       ->prototype('array')
          ->children()
              ->arrayNode('options')
                  ->children()
                  ->scalarNode('name')->end()
                  ->scalarNode('id')->end()
                  ->scalarNode('type')->end()
                  ->scalarNode('error')->end()
              ->end()
           ->end()
     ->end()
->end()

That way you can define as many services as you want as long as they follow this pattern.

Upvotes: 2

Chopchop
Chopchop

Reputation: 2949

You can try :

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

    $rootNode
        ->children()
            // also removed the ->end() for each arrayNode but then I get a Fatal Error 
            ->arrayNode('acme_services')
                 ->children()
                      ->arrayNode('service_a')
                          ->children()
                              ->arrayNode('options')->end()
                      ->arrayNode('service_b')
                          ->children()
                              ->arrayNode('options')->end()
           ->end();

    return $treeBuilder;
}

Upvotes: 1

Related Questions