@@ -196,44 +196,157 @@ Array Node Options
196
196
Before defining the children of an array node, you can provide options like:
197
197
198
198
``useAttributeAsKey() ``
199
- Provide the name of a child node, whose value should be used as the
200
- key in the resulting array.
199
+ Provide the name of a child node, whose value should be used as the key in
200
+ the resulting array. This method also defines the way config array keys are
201
+ treated, as explained in the following example.
201
202
``requiresAtLeastOneElement() ``
202
203
There should be at least one element in the array (works only when
203
204
``isRequired() `` is also called).
204
205
``addDefaultsIfNotSet() ``
205
206
If any child nodes have default values, use them if explicit values
206
207
haven't been provided.
207
208
208
- An example of this ::
209
+ A basic prototyped array configuration can be defined as follows ::
209
210
210
- $rootNode
211
+ $node
212
+ ->fixXmlConfig('driver')
211
213
->children()
212
- ->arrayNode('parameters')
213
- ->isRequired()
214
- ->requiresAtLeastOneElement()
215
- ->useAttributeAsKey('name')
214
+ ->arrayNode('drivers')
215
+ ->prototype('scalar')->end()
216
+ ->end()
217
+ ->end()
218
+ ;
219
+
220
+ When using the following YAML configuration:
221
+
222
+ .. code-block :: yaml
223
+
224
+ drivers : ['mysql', 'sqlite']
225
+
226
+ Or the following XML configuration:
227
+
228
+ .. code-block :: xml
229
+
230
+ <driver >msyql</driver >
231
+ <driver >sqlite</driver >
232
+
233
+ The processed configuration is::
234
+
235
+ Array(
236
+ [0] => 'mysql'
237
+ [1] => 'sqlite'
238
+ )
239
+
240
+ A more complex example would be to define a prototyped array with children::
241
+
242
+ $node
243
+ ->fixXmlConfig('connection')
244
+ ->children()
245
+ ->arrayNode('connections')
216
246
->prototype('array')
217
247
->children()
218
- ->scalarNode('value')->isRequired()->end()
248
+ ->scalarNode('table')->end()
249
+ ->scalarNode('user')->end()
250
+ ->scalarNode('password')->end()
219
251
->end()
220
252
->end()
221
253
->end()
222
254
->end()
223
255
;
224
256
225
- In YAML, the configuration might look like this :
257
+ When using the following YAML configuration :
226
258
227
259
.. code-block :: yaml
228
260
229
- database :
230
- parameters :
231
- param1 : { value: param1val }
261
+ connections :
262
+ - { table: symfony, user: root, password: ~ }
263
+ - { table: foo, user: root, password: pa$$ }
264
+
265
+ Or the following XML configuration:
266
+
267
+ .. code-block :: xml
268
+
269
+ <connection table =" symfony" user =" root" password =" null" />
270
+ <connection table =" foo" user =" root" password =" pa$$" />
271
+
272
+ The processed configuration is::
273
+
274
+ Array(
275
+ [0] => Array(
276
+ [table] => 'symfony'
277
+ [user] => 'root'
278
+ [password] => null
279
+ )
280
+ [1] => Array(
281
+ [table] => 'foo'
282
+ [user] => 'root'
283
+ [password] => 'pa$$'
284
+ )
285
+ )
286
+
287
+ The previous output matches the expected result. However, given the configuration
288
+ tree, when using the following YAML configuration:
289
+
290
+ .. code-block :: yaml
291
+
292
+ connections :
293
+ sf_connection :
294
+ table : symfony
295
+ user : root
296
+ password : ~
297
+ default :
298
+ table : foo
299
+ user : root
300
+ password : pa$$
301
+
302
+ The output configuration will be exactly the same as before. In other words, the
303
+ ``sf_connection `` and ``default `` configuration keys are lost. The reason is that
304
+ the Symfony Config component treats arrays as lists by default.
305
+
306
+ In order to maintain the array keys use the ``useAttributeAsKey() `` method::
307
+
308
+ $node
309
+ ->fixXmlConfig('connection')
310
+ ->children()
311
+ ->arrayNode('connections')
312
+ ->prototype('array')
313
+ ->useAttributeAsKey('name')
314
+ ->children()
315
+ ->scalarNode('table')->end()
316
+ ->scalarNode('user')->end()
317
+ ->scalarNode('password')->end()
318
+ ->end()
319
+ ->end()
320
+ ->end()
321
+ ->end()
322
+ ;
323
+
324
+ The argument of this method (``name `` in the example above) defines the name of
325
+ the attribute added to each XML node to differentiate them. Now you can use the
326
+ same YAML configuration showed before or the following XML configuration:
327
+
328
+ .. code-block :: xml
232
329
233
- In XML, each ``parameters `` node would have a ``name `` attribute (along
234
- with ``value ``), which would be removed and used as the key for that element
235
- in the final array. The ``useAttributeAsKey `` is useful for normalizing
236
- how arrays are specified between different formats like XML and YAML.
330
+ <connection name =" sf_connection"
331
+ table =" symfony" user =" root" password =" null" />
332
+ <connection name =" default"
333
+ table =" foo" user =" root" password =" pa$$" />
334
+
335
+ In both cases, the processed configuration maintains the ``sf_connection `` and
336
+ ``default `` keys::
337
+
338
+ Array(
339
+ [sf_connection] => Array(
340
+ [table] => 'symfony'
341
+ [user] => 'root'
342
+ [password] => null
343
+ )
344
+ [default] => Array(
345
+ [table] => 'foo'
346
+ [user] => 'root'
347
+ [password] => 'pa$$'
348
+ )
349
+ )
237
350
238
351
Default and Required Values
239
352
---------------------------
0 commit comments