Skip to content

Commit e77c3b2

Browse files
committed
Rewritten the explanation about the useAttributeAsKey() method
1 parent 0f8f9fd commit e77c3b2

File tree

1 file changed

+109
-34
lines changed

1 file changed

+109
-34
lines changed

components/config/definition.rst

Lines changed: 109 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -211,72 +211,147 @@ Before defining the children of an array node, you can provide options like:
211211
the resulting array. This method also defines the way config array keys are
212212
treated, as explained in the following example.
213213

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::
217215

218-
$rootNode
216+
$node
217+
->fixXmlConfig('driver')
219218
->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()
227221
->end()
228222
->end()
229223
;
230224

231-
In YAML, the configuration might look like this:
225+
When using the following YAML configuration:
232226

233227
.. code-block:: yaml
234228
235-
database:
236-
parameters: [ 'value1', 'value2' ]
229+
drivers: ['mysql', 'sqlite']
237230
238-
In XML, the configuration might look like this:
231+
Or the following XML configuration:
239232

240233
.. code-block:: xml
241234
242-
...
235+
<driver>msyql</driver>
236+
<driver>sqlite</driver>
243237
244-
However, if the ``useAttributeAsKey()`` method is set, the parsed configuration
245-
will be completely different::
238+
The processed configuration is::
246239

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')
248249
->children()
249-
->arrayNode('parameters')
250-
->useAttributeAsKey('value')
250+
->arrayNode('connections')
251251
->prototype('array')
252252
->children()
253-
->scalarNode('parameter1')->end()
254-
->scalarNode('parameter2')->end()
253+
->scalarNode('table')->end()
254+
->scalarNode('user')->end()
255+
->scalarNode('password')->end()
255256
->end()
256257
->end()
257258
->end()
258259
->end()
259260
;
260261

261-
In YAML, the configuration might look like this:
262+
When using the following YAML configuration:
262263

263264
.. code-block:: yaml
264265
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$$ }
269269
270-
In XML, the configuration might look like this:
270+
Or the following XML configuration:
271271

272272
.. code-block:: xml
273273
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
275334
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+
)
280355

281356
Default and required Values
282357
---------------------------

0 commit comments

Comments
 (0)