简体   繁体   English

如何在Symfony2配置中添加包含值的数组?

[英]How to add an array with values in Symfony2 configuration?

I would like to add a simple list of values in a configuration files (config.yml). 我想在配置文件(config.yml)中添加一个简单的值列表。 For example : 例如 :

my_bundle:
    columns: ["col1", "col2"]

When adding the node to the configuration parser, it simply fails : 将节点添加到配置解析器时,它只是失败:

$rootNode = $treeBuilder->root('my_bundle');
$rootNode->arrayNode('columns')->children()->end();

Here is the error : 这是错误:

InvalidConfigurationException: Unrecognized options "0, 1" under "my_bundle.columns"

What am I missing? 我错过了什么? Is this even possible? 这甚至可能吗?

If you want to achieve a node like this, just do: 如果你想实现这样的节点,那就做:

$rootNode
    ->children()
        ->arrayNode('columns')
            ->prototype('scalar')
            ->end()
        ->end()
    ->end()
;

I think you're missing that YaML is not a markup language, it's not even a langauge as such (It's a data serialization standard), so it doesn't know of any language constructs, like arrays. 我认为你缺少 YAML不是一种标记语言,它甚至不是一个的langauge这样(这是一个数据序列化标准),所以它不知道任何语言构造,如阵列。 Its main "tool" to express grouping and relations between bits of data is whitespace, colons and dashes. 它表达分组和数据位之间关系的主要“工具”是空格,冒号和短划线。
From the symfony documentation page of the YaML format : YaML格式symfony文档页面

my_bundle:
    columns: 
        - col1
        - col2

As I gather from this section: 我从这一部分收集到:

A YAML file is rarely used to describe a simple scalar. YAML文件很少用于描述简单的标量。 Most of the time, it describes a collection. 大多数时候,它描述了一个集合。 A collection can be a sequence or a mapping of elements. 集合可以是元素的序列或映射。 Both sequences and mappings are converted to PHP arrays. 序列和映射都转换为PHP数组。

Sequences use a dash followed by a space: 序列使用破折号后跟空格:

- PHP - PHP

- Perl - Perl

- Python - Python

The previous YAML file is equivalent to the following PHP code: 以前的YAML文件等效于以下PHP代码:

array('PHP', 'Perl', 'Python');

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

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