2012-06-07 20 views
20

基本上我想允許的任意的(但不)的我的配置鍵 - 值對,下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; 
    } 
} 

回答

21

您應該將useAttributeAsKey('name')添加到您的結算節點配置Configurator.php中。

useAttributeAsKey()你可以在 Symfony API Documentation

讀取之後改變計費節點的配置應該是這樣

的更多信息:

->arrayNode('billings') 
    ->isRequired() 
    ->requiresAtLeastOneElement() 
    ->useAttributeAsKey('name') 
    ->prototype('scalar')->end() 
->end() 
+0

感謝,我給它一個嘗試,並報告! – gremo

+0

如果我想避免使用'isRequired'並在null爲空時提供默認數組? –

+1

我正確的在' - > useAttributeAsKey('name')''name'有特殊含義,或者它至少自動添加到數組中? – martin

1

我最近不得不建立一些嵌套陣列配置。

需求是

  • 第一級陣列與用戶鍵(discribing實體路徑)
  • 每個那些陣列必須具有一個或多個任意的標籤作爲鍵,用布爾值。

要實現這樣的配置,您必須使用casacade prototype()方法。

所以,以下配置:

$treeBuilder = new TreeBuilder(); 
$rootNode = $treeBuilder->root('my_bundle'); 
$rootNode 
    ->addDefaultsIfNotSet() # may or may not apply to your needs 
    ->performNoDeepMerging() # may or may not apply to your needs 
    ->children() 
     ->scalarNode('some_scalar') 
      ->info("Some useful tips ..") 
      ->defaultValue('default') 
      ->end() 
     ->arrayNode('first_level') 
      ->info('Useful tips here..') 
      # first_level expects its children to be arrays 
      # with arbitrary key names 
      ->prototype('array') 
       # Those arrays are expected to hold one or more boolean entr(y|ies) 
       # with arbitrary key names 
       ->prototype('boolean') 
        ->defaultFalse()->end() 
       ->end() 
      ->end() 
     ->end() 
    ->end(); 
return $treeBuilder; 

從擴展類內傾倒$配置給出以下輸出:

Array 
(
    [some_scalar] => my_scalar_value 
    [first_level] => Array 
     (
      [AppBundle:User] => Array 
       (
        [first_tag] => 
       ) 

      [AppBundle:Admin] => Array 
       (
        [second_tag] => 1 
        [third_tag] => 
       ) 
     ) 
) 

my_bundle: 
    some_scalar: my_scalar_value 
    first_level: 
     "AppBundle:User": 
      first_tag: false 
     "AppBundle:Admin": 
      second_tag: true 
      third_tag: false 

可以使用以下配置來獲得

而且瞧!

0

這是useAttributeAsKey實際上做:

/** 
* Sets the attribute which value is to be used as key. 
* 
* This is useful when you have an indexed array that should be an 
* associative array. You can select an item from within the array 
* to be the key of the particular item. For example, if "id" is the 
* "key", then: 
* 
*  array(
*   array('id' => 'my_name', 'foo' => 'bar'), 
* ); 
* 
* becomes 
* 
*  array(
*   'my_name' => array('foo' => 'bar'), 
* ); 
* 
* If you'd like "'id' => 'my_name'" to still be present in the resulting 
* array, then you can set the second argument of this method to false. 
* 
* This method is applicable to prototype nodes only. 
* 
* @param string $name   The name of the key 
* @param bool $removeKeyItem Whether or not the key item should be removed 
* 
* @return ArrayNodeDefinition 
*/ 
public function useAttributeAsKey($name, $removeKeyItem = true) 
{ 
    $this->key = $name; 
    $this->removeKeyItem = $removeKeyItem; 

    return $this; 
} 
相關問題