基本上我想允許的任意的(但不空)的我的配置鍵 - 值對,下billings
節點數量,即定義一個聯想陣列。允許在Symfony的2束語義結構的鍵 - 值對
我在Configurator.php
(的一部分)已經這樣:
->arrayNode('billings')
->isRequired()
->requiresAtLeastOneElement()
->prototype('scalar')
->end()
->end()
,然後在我的config.yml
:
my_bundle:
billings:
monthly : Monthly
bimonthly : Bimonthly
然而,輸出$config
:
class MyBundleExtension extends Extension
{
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$loader = new Loader\YamlFileLoader($container,
new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
$processor = new Processor();
$configuration = new Configuration();
$config = $processor->processConfiguration($configuration, $configs);
$container->setParameter('my_bundle.billings', $config['billings']);
var_dump($config);
die();
}
}
。 ..我得到的是數組索引,不是關聯一個:
'billings' =>
array (size=2)
0 => string 'Monthly' (length=7)
1 => string 'Bimonthly' (length=9)
出於好奇(如果這能幫助),我試圖注入這個數組作爲一個服務參數(註釋從這個大捆:JMSDiExtraBundle):
class BillingType extends \Symfony\Component\Form\AbstractType
{
/**
* @var array
*/
private $billingChoices;
/**
* @InjectParams({"billingChoices" = @Inject("%billings%")})
*
* @param array $billingChoices
*/
public function __construct(array $billingChoices)
{
$this->billingChoices = $billingChoices;
}
}
感謝,我給它一個嘗試,並報告! – gremo
如果我想避免使用'isRequired'並在null爲空時提供默認數組? –
我正確的在' - > useAttributeAsKey('name')''name'有特殊含義,或者它至少自動添加到數組中? – martin