2017-09-23 9 views
0

, 나는이 오류 받고 있어요 : 여기읽기 CSV 파일에서 노드와 - 나는 일괄 처리를 사용하여 노드를 저장하기 위해 노력하고있어 때 드루팔 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);

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

가 누군가를 도움이되기를 바랍니다 :

답변

1

문제는 등, ö 같은 파일

그래서 솔루션이 값 utf8_encode()을 사용했다 CSV에서 영어 이외의 문자였다.

0

하기이 같은 모든 값에 대한 변환을 추가 할 수 있습니다

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