简体   繁体   English

Symfony2 yaml数组选项

[英]Symfony2 yaml array options

In app/config/config.yml I've added some custom settings for my bundle app/config/config.yml我为我的包添加了一些自定义设置

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 ? 现在在src/ACME/Bundle/ACMEBundle/DependencyInjection/Configuration.php如何设置默认和/或检查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 . 所以我需要拉取service_aservice_b数组,但是我收到了service_aservice_b Unrecognized options错误。

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 . 期望的结果是我想在acme_services数组中同时拥有service_aservice_b ,这就是为什么我可以针对acme_services数组验证所使用的服务, service_aservice_b

Note: In PHP I would write it like this: (not sure if this is correct but it's an example) 注意:在PHP中我会这样写:(不确定这是否正确,但这是一个例子)

$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',
            )
        )
);

What you want to use is Prototypes , something like: 您想要使用的是Prototypes ,类似于:

$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. 这样,只要遵循此模式,您就可以根据需要定义任意数量的服务。

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

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM