使用Symfony2的配置类,如何定义他们的孩子没有数组中的键节点?(Using the Symfo

2019-07-30 22:59发布

使用配置类,如何定义没有数字键的排列节点? 数组的孩子并不代表进一步的配置选项。 相反,他们会不会是能够选择性覆盖,只有作为一个整体的列表。

到目前为止,我有:

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

app/config.yml ,我希望能够覆盖它是这样的:

acme_base:
  entities:
    - 'Acme\BaseBundle\Entity\AnotherEntity1'
    - 'Acme\BaseBundle\Entity\AnotherEntity2'
    - 'Acme\BaseBundle\Entity\AnotherEntity3'
    - 'Acme\BaseBundle\Entity\AnotherEntity4'

Answer 1:

我认为你需要

$root
    ->children()
        ->arrayNode('entities')
        ->addDefaultsIfNotSet()
        ->prototype('scalar')->end()
        ->defaultValue(array(
            'Acme\BaseBundle\Entity\DefaultEntity1',
            'Acme\BaseBundle\Entity\DefaultEntity2',
        ))
    ->end()


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