2017-09-23 123 views
0

當我嘗試使用批處理保存節點,我得到這個錯誤:讀取節點,節省使用批處理 - Drupal的7

PDOException: in drupal_write_record() (line 7383 of C:\wamp64\www\drupal7\includes\common.inc).

這裏是我的完整代碼:

function custom_node_import_form_submit($form, &$form_state) { 
    $csvFile = file_load($form_state['values']['csv_file']); 
    $csvFilepath = drupal_realpath($csvFile->uri); 
    $file = fopen($csvFilepath, "r"); 

    $batch = array(
    'operations' => array(), 
    'finished' => 'node_import_finished', 
    'title' => t('Node import'), 
    'init_message' => t('Importing is starting...'), 
    'progress_message' => t('Imported @current out of @total.'), 
    'error_message' => t('Node importer has encountered an error.') 
); 

    fgetcsv($file, 0, ","); 
    while($line = fgetcsv($file)) { 
    $batch['operations'][] = array('node_import_progress', array(array_map('base64_encode', $line))); 
    } 

    batch_set($batch); 
    batch_process('admin/node/custom-node-import'); 
    fclose($file); 
} 

function node_import_progress($line, &$context) { 
    $line = array_map('base64_decode', $line); 
    saveNode($line); 
    $context['message'] = t('Importing %title', array('%title' => $line[0])); 
} 

function node_import_finished($success, $results, $operations) { 
    if ($success) { 
    drupal_set_message(t('Node importing is complete!')); 
    } 
    else { 
    $error_operation = reset($operations); 
    $message = t('An error occurred while processing %error_operation with arguments: @arguments', array(
     '%error_operation' => $error_operation[0], 
     '@arguments' => print_r($error_operation[1], TRUE) 
    )); 
    drupal_set_message($message, 'error'); 
    } 
} 
function saveNode($param = []) { 
    global $user; 

    $node = new stdClass(); 
    $node->title = $param[0]; 
    $node->type = "article"; 
    node_object_prepare($node); 
    $node->language = LANGUAGE_NONE; 
    $node->uid = $user->uid; 
    $node->status = 1; 
    $node->promote = 0; 
    $node->comment = 1;  
    $node->body[$node->language][]['value'] = $param[3]; 

    $node = node_submit($node); 
    node_save($node); 
} 

我調試過,發現出現這個錯誤的那一行是節點最後一行被保存的地方。即node_save($ node);

回答

1

問題同在CSV非英語字符的文件像O,A等

因此,解決辦法是使用utf8_encode()的價值觀:

$node->title = utf8_encode($param[0]); 
$node->body[$node->language][]['value'] = utf8_encode($param[3]); 

希望它可以幫助別人。

0

,你還可以添加一個轉換爲這樣的價值觀:

array_walk($param, function(&$value){ 
    $value = mb_convert_encoding($value, 'UTF-8', mb_detect_encoding($value)); 
});