How to replace a flexform field name without loosing data
Many years ago when I just started to work with TemplaVoila, I did my objects the same way as many people do: separate DS/TO pairs even if objects are similar. For example, on one site I had three types of recipes and I had three separate DS/TO pairs for them. I should have made a single data source and three different template objects instead.
It would not be difficult to change all template objects to use the same data source (just replace data source id for all flexible content elements). The real problem was that fields in data sources were named differently. The data was similar but filed names were different.
How to solve the problem in the most efficient way? Please, stop reading now and think.
* * *
If you thought about an extension, it is not a most efficient way. Stop and think again!
* * *
If you thought about writing a PHP script that will fetch all rows and replace a string using PHP's str_replace, than it is not the most efficient either. Think again!
* * *
The best way is to use mysql command line utility to do this query: UPDATE tt_content SET tx_templavoila_flex= REPLACE(tx_templavoila_flex, 'field_old', 'field_new') WHERE tx_templavoila_ds=123;
This runs quickly: nearly seconds on the tt_content table with approximately 36000 rows.
Now you can replace a data source with another simple statement: UPDATE tt_content SET tx_templavoila_ds=456 WHERE tx_templavoila_ds=123;
Or combine two into one: UPDATE tt_content SET tx_templavoila_ds=456, tx_templavoila_flex=REPLACE(tx_templavoila_flex, 'field_old', 'field_new') WHERE tx_templavoila_ds=123;
Voila! No extension, no PHP script. The whole update takes less than thirty seconds and there is no need to debug any PHP! This is what I call "Never rush but think first!"
In my case I did not have to replace a data source immediately. It needs further small adjustments. I only had to unify all fields. In my case two extra steps were necessary:
Go to the data source and edit XML to change filed_old to field_new
Map new field to the place in HTML where old field was mapped
The above took me extra two minutes. Compare it to the time necessary for the extension writing. It clearly shows that thinking first really saves a lot of time.