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