Symfony 2 - 配置值:关联数组数组

时间:2012-12-17 23:18:36

标签: symfony

我想处理以下配置:

acme_demo:
    transitions:
        - { hc_cba: 180 }
        - { cba_hc: -1 }

很明显,我需要添加一个arrayNode,比如

$rootNode
    ->children()
        ->arrayNode('transitions')
            ->beforeNormalization()
                ->ifArray()
                    ->then(function($values) {
                        return array('transition' => $values);
                    })
                ->end()
            ->end()
        ->end()
    ->end()
;

但是这给了我带有消息

的InvalidConfigurationException
Unrecognized options "transitions" under "acme_demo.state_machine"

我应该如何处理这些“内在”价值观?

1 个答案:

答案 0 :(得分:3)

在写下这个问题之后,经过数小时尝试后的大量压力驱使我找到解决方案:

$rootNode
        ->children()
            ->arrayNode('state_machine')
                ->requiresAtLeastOneElement()
                ->beforeNormalization()
                    ->ifArray()
                        ->then(function($values) {
                            $ret = array();

                            foreach ($values as $value) {
                                foreach ($value as $transition => $time) {
                                    $ret[] = array('transition' => $transition, 'time' => e);
                                }
                            }

                            return $ret;
                        })
                    ->end()
                    ->prototype('array')
                    ->children()
                        ->scalarNode('transition')->end()
                        ->scalarNode('time')->end()
                    ->end()
                ->end()
            ->end()
        ->end()
    ;