如何在 Symfony Configuration.php 中定义标量数组?
How do I define an array of arrays of scalars in Symfony Configuration.php?
我想解析如下所示的 YAML 配置:
pageRoles:
Report1: [abc, xyz, def]
Report2: [fgh, xxx, yyy, rrr]
我希望生成的配置数组如下所示:
'pageRoles':
'Report1':
[
'abc',
'xyz',
'def'
],
'Report2':
[
'fgh',
'xxx',
'yyy',
'rrr'
]
我现在有这个:
->arrayNode( 'pageRoles' )
->prototype( 'array' )
->useAttributeAsKey( 'name' )
->prototype( 'array' )
->prototype( 'scalar' )->end()
->end()
->end() // array prototype
->end() // pageRoles
我收到此错误:
Invalid type for path "site.pageRoles.ActivityReport.0". Expected
array, but got string
我错过了什么?
制作 Symfony 配置树。我最喜欢的消磨无聊一天的方式。这似乎有效:
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('my');
$treeBuilder->getRootNode()
->children()
->arrayNode('pageRoles')
->useAttributeAsKey('name')
->arrayPrototype()->scalarPrototype()->end()->end()
->end() // pageRoles
->end() // root node
;
return $treeBuilder;
}
处理后我得到:
array:1 [
"pageRoles" => array:2 [
"Report1" => array:3 [
0 => "abc"
1 => "xyz"
2 => "def"
]
"Report2" => array:4 [
0 => "fgh"
1 => "xxx"
2 => "yyy"
3 => "rrr"
]
]
]
我想解析如下所示的 YAML 配置:
pageRoles:
Report1: [abc, xyz, def]
Report2: [fgh, xxx, yyy, rrr]
我希望生成的配置数组如下所示:
'pageRoles':
'Report1':
[
'abc',
'xyz',
'def'
],
'Report2':
[
'fgh',
'xxx',
'yyy',
'rrr'
]
我现在有这个:
->arrayNode( 'pageRoles' )
->prototype( 'array' )
->useAttributeAsKey( 'name' )
->prototype( 'array' )
->prototype( 'scalar' )->end()
->end()
->end() // array prototype
->end() // pageRoles
我收到此错误:
Invalid type for path "site.pageRoles.ActivityReport.0". Expected array, but got string
我错过了什么?
制作 Symfony 配置树。我最喜欢的消磨无聊一天的方式。这似乎有效:
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('my');
$treeBuilder->getRootNode()
->children()
->arrayNode('pageRoles')
->useAttributeAsKey('name')
->arrayPrototype()->scalarPrototype()->end()->end()
->end() // pageRoles
->end() // root node
;
return $treeBuilder;
}
处理后我得到:
array:1 [
"pageRoles" => array:2 [
"Report1" => array:3 [
0 => "abc"
1 => "xyz"
2 => "def"
]
"Report2" => array:4 [
0 => "fgh"
1 => "xxx"
2 => "yyy"
3 => "rrr"
]
]
]