Step 8 - Data migration between Field Type versions¶
Adding data migration enables you to easily change the output of the Field Type to fit your current needs. This process is important when a Field Type needs to be compared for sorting and searching purposes. Serialization allows changing objects to array by normalizing them, and then to the selected format by encoding them. In reverse, deserialization changes different formats into arrays by decoding and then denormalizing them into objects.
For more information on Serializer Components, see Symfony documentation.
Normalization¶
First, you need to add support for normalization in a src/Serializer/Point2D/ValueNormalizer.php
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Add Normalizer definition¶
Next, add the ValueNormalizer
service definition to the config/services.yaml
with a serializer.normalizer
tag:
1 2 3 4 |
|
Backward compatibility¶
To accept old versions of the Field Type you need to add support for denormalization in a src/Serializer/Point2D/ValueDenormalizer.php
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
Add Denormalizer definition¶
Next, add the ValueDenormalizer
service definition to config/services.yaml
with a serializer.denormalizer
tag:
1 2 3 4 |
|
Change format on the fly¶
To change the format on the fly, you need to replace the constructor in src/FieldType/Point2D/Value.php
:
1 2 3 4 5 6 7 |
|
Now you can easily change the internal representation format of the Point 2D Field Type.