Migrate content node & tags from Drupal 6 to Drupal 7 Programmatically Part III
Migrating node content by mysql query give some problem in the next development. The best way to moving content nodes and taxonomy attached with it by using drupal API. Yes, programmatically is the best approach and save if we want moving all contents from Drupal 6 into Drupal 7. Using raw moving like my previous article Part I will contains error when we post a new content. The error is like this :duplicate entry '0' for key 'vid': insert into {node} (vid, type, language, title, uid, status, created, changed, comment, promote, sticky) values (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9
Before, you need to truncate several tables in Drupal 7 :
TRUNCATE `apachesolr_search_node` ;
TRUNCATE `cache_field` ;
TRUNCATE `cache_menu` ;
TRUNCATE `field_data_body` ;
TRUNCATE `field_data_field_tags` ;
TRUNCATE `field_revision_body` ;
TRUNCATE `field_revision_field_tags` ;
TRUNCATE `node` ;
TRUNCATE `node_comment_statistics` ;
TRUNCATE `node_revision` ;
TRUNCATE `taxonomy_index` ;
TRUNCATE `url_alias` ;Now, here are script to moving nodes from Drupal 6 to Drupal with taxonomy attached programmatically :
<?php
// Yodiaditya - yoodey.com - migrate nodes
function migrate_post_node() {
db_set_active('old');
$result = db_query("SELECT uid, nid, title, created, changed, promote, sticky, vid FROM {node} WHERE type = 'story' ORDER BY nid ASC ");
db_set_active('default');
foreach(
$result as $record){
/* PICKING TERMS , you can query into your old taxonomy and get tid */
$term = 1;
if(empty(
$record)){
return false;
}else{
db_set_active('old');
$revisions = db_select('node_revisions')->fields('node_revisions')->condition('nid',$record->nid)->range(0,1)->execute()->fetchObject();
db_set_active('default');
$node = new stdClass();
$node->title = $record->title;
$node->type = 'article';
$node->created = $record->created;
$node->changed= $record->changed;
$node->comment = 1;
$node->status = 1;
$node->promote = 1;
$node->sticky = 0;
$node->uid = $record->uid;
$node->language = 'und';
$node->timestamp = $revisions->timestamp;
$node->revision = 0;
$node->body['und'][0]['summary'] = $revisions->teaser;
$node->field_tags['und'][0] = array('tid' => $term);
$node->body['und'][0]['value'] = $revisions->body;
$node->body['und'][0]['format'] = 'full_html';
node_submit($node);
node_save($node);
}
}
echo
'1';
}
?>See my previous article to know what first steps to know, for example : Usage of multiple database connection in Drupal 7.
Done! This is the easy and safe ways to migrate all nodes from Drupal 6 into Drupal 7.
Comments
Konrad (not verified)
Sun, 05/15/2011 - 02:33
Permalink
Problem with node_submit($node); when $node->date is not set.
This and other tutorials saved me a lot of time while migrating Forums. Thank you! Just one patch for the code above.
Attribute $node->date must be set before node_submit():
$node->date = format_date($node->created, 'custom', 'Y-m-d H:i:s O');Otherwise $node->created is set to REQUEST_TIME.
http://api.drupal.org/api/drupal/modules--node--node.module/function/nod...
Thanks once again!
Add new comment