I want to configure a bundle to allow different behavior for different companies. The config structure within them will be the same.
My config.yml
shall look like this:
bunde_namespace:
company:
company_1:
foo: bar
baz: poit
company_2:
foo: bar
baz: poit
company_3:
...
When I access the $config
I expect the array to look something like this:
$config['company'] = [
'company_one' => [
'foo' => 'bar'
'baz' => 'poit'
],
'company_two' => [
'foo' => 'bar'
'baz' => 'poit'
],
...
];
Yet I have no experience with the TreeBuilder and setting up the configuration as described in the docs and it still eludes me as to how I setup my configuration so that it treats the children of company
as keyed arrays.
What I achieved so far is to setup the config for one company, like so:
class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('dreamlines_booking_service_fibos');
$rootNode
->children()
->arrayNode('company')
->children()
->scalarNode('foo')->end()
->scalarNode('baz')->end()
->end()
->end()
->end();
return $treeBuilder;
}
}
And the simplified config.yml
would look like this:
bundle_namespace:
company:
foo: bar
baz: poit
Yet this is not what I want.
I am assuming that I need to use useAttributeAsKey
yet I have trouble getting it work.
This fails:
$rootNode
->children()
->arrayNode('company')
->prototype('array')
->useAttributeAsKey('name')
->children()
->scalarNode('foo')->end()
->scalarNode('baz')->end()
->end()
->end()
->end()
->end();
stating:
[SymfonyComponentConfigDefinitionExceptionInvalidDefinitionException]
->useAttributeAsKey() is not applicable to concrete nodes at path "bundle_namespace."
Where am I going wrong?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…