Linda A - 17 December 2008 04:40 PM
I don’t suppose its possible to extend it with some kind of migration utility for existing sites that already have fields repeated over several custom field groups?
Here’s a little script I just wrote that will copy all of your field data from one field to another:
<?php
// -------------------
// Configuration
// -------------------
// Old Field Names
$old_fields = array(
'old_field_1',
'old_field_2',
'old_field_3'
// etc.
);
// Gypsy Field Name
$gypsy_field = 'gypsy_field';
// Database Server
$db_server = 'localhost';
// Database User Name
$db_username = 'username';
// Database User Password
$db_password = 'password';
// Database Name
$db_name = 'my_website';
// -------------------
// Script
// -------------------
// Connect to the database
$db_link = mysql_connect($db_server, $db_username, $db_password)
OR exit('Could not connect to MySQL.');
mysql_select_db($db_name, $db_link)
OR exit('Could not connect to the database.');
// Get the Gypsy field's ID
$gypsy_field_result = mysql_query("SELECT field_id
FROM exp_weblog_fields
WHERE field_name = '{$gypsy_field}'", $db_link) OR exit(mysql_error());
if ( ! mysql_num_rows($gypsy_field_result))
exit("No field exists with the name \"{$gypsy_field}\"");
$gypsy_field_id = 'field_id_'.mysql_result($gypsy_field_result, 0);
// Get the old fields' IDs
$old_fields_result = mysql_query("SELECT field_id
FROM exp_weblog_fields
WHERE field_name IN ('".implode(',', $old_fields)."')", $db_link) OR exit(mysql_error());
if ( ! mysql_num_rows($old_fields_result))
exit('No fields exist with the names "'.implode('", "', $old_fields).'"');
// Update Gypsy field's data with content from each old field
while($row = mysql_fetch_row($old_fields_result)) {
$old_field_id = "field_id_{$row[0]}";
if ($old_field_id == $gypsy_field_id)
continue;
$data_result = mysql_query("SELECT entry_id, {$old_field_id} data
FROM exp_weblog_data
WHERE {$old_field_id} != ''", $db_link) OR exit(mysql_error());
if ( ! mysql_num_rows($data_result))
continue;
while($data_row = mysql_fetch_assoc($data_result)) {
mysql_query("UPDATE exp_weblog_data
SET {$gypsy_field_id} = '".addslashes($data_row['data'])."'
WHERE entry_id = '{$data_row['entry_id']}'", $db_link) OR exit(mysql_error());
}
}
echo "Success!";
?>
First go into your CP and create your new gypsy field. Then save that script as a PHP file, change the Configuration variables at the top, upload it to your website, and access it from your browser. (And when you’re done migrating, delete the old fields AND DELETE THAT SCRIPT!)