2012-06-07 12 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 outputting:

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

आपको Configurator.php में अपने बिलिंग नोड कॉन्फ़िगरेशन में useAttributeAsKey('name') जोड़ना चाहिए।

अधिक useAttributeAsKey() आप Symfony API Documentation

पर पढ़ सकते हैं परिवर्तन के बाद बिलिंग नोड विन्यास की तरह यह होना चाहिए के बारे में जानकारी: उसके बाद

->arrayNode('billings') 
    ->useAttributeAsKey('whatever') 
    ->prototype('scalar')->end() 

:

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

धन्यवाद, मैं इसे आज़माने रहा हूँ और वापस रिपोर्ट! – gremo

+0

यदि मैं 'isRequired' से बचना चाहता हूं और शून्य के मामले में डिफ़ॉल्ट सरणी प्रदान करना चाहता हूं तो क्या होगा? –

+1

क्या मैं सही हूं कि '-> useAttributeAsKey (' name ')' 'name' 'में विशेष अर्थ है या यह कम से कम सरणी में जोड़ा गया है? – martin

1

मैं हाल ही में स्थापना के लिए कुछ नेस्टेड सरणियों विन्यास था।

आवश्यकताओं कस्टम कुंजी (एक इकाई पथ discribing)

  • उन सरणियों से प्रत्येक बूलियन मान के साथ, कुंजी के रूप में एक या अधिक मनमाना टैग करवाना पड़ा साथ

    • एक प्रथम स्तर सरणी थे।

    ऐसी कॉन्फ़िगरेशन प्राप्त करने के लिए, आपको prototype() विधि को कैसाक करना होगा।

    तो, निम्नलिखित विन्यास:

    Array 
    (
        [some_scalar] => my_scalar_value 
        [first_level] => Array 
         (
          [AppBundle:User] => Array 
           (
            [first_tag] => 
           ) 
    
          [AppBundle:Admin] => Array 
           (
            [second_tag] => 1 
            [third_tag] => 
           ) 
         ) 
    ) 
    
    :

    $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; 
    

    विस्तार वर्ग अंदर से $ config डम्पिंग निम्नलिखित उत्पादन देता है:

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

    निम्नलिखित विन्यास का उपयोग कर प्राप्त किया जा सकता

    और voilà!

  • 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; 
    } 
    
    संबंधित मुद्दे