17

I would like to add a simple list of values in a configuration files (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?

2 Answers 2

40

If you want to achieve a node like this, just do:

$rootNode
    ->children()
        ->arrayNode('columns')
            ->prototype('scalar')
            ->end()
        ->end()
    ->end()
;
Sign up to request clarification or add additional context in comments.

3 Comments

Nearly. I found another question mentioning the prototype method. In my case, the prototype has to hold scalar and it worked.
I don't understand what you're trying to achieve then, using this code var_dump($config); = Array ( [columns] => Array ( [0] => col1 [1] => col2 ) )
Shouldn't there also be another ->end() right after ->prototype('scalar')?
0

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. 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:

my_bundle:
    columns: 
        - col1
        - col2

As I gather from this section:

A YAML file is rarely used to describe a simple scalar. 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.

Sequences use a dash followed by a space:

- PHP

- Perl

- Python

The previous YAML file is equivalent to the following PHP code:

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

3 Comments

["PHP", "Perl", "Python"] is equivalent to your notation in YAML. And what I was asking is indeed possible as Touki answered it. All you have to do is specify which kind of data the array contains. In my case string/scalar.
@i.am.michiel: misread the question, in that case. I thought ['col1', 'col2'] wasn't working, have you tried explicitly setting the type, too? - !!str col1?
No problem. And the parsing problem was not on the YAML-side but on the Symfony-side. All I had to do was add a conversion option in the YAML-parser as noted in Loki's answer.

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.