CSV 数据未写入数据库、SuiteCRM、自定义导入脚本

CSV Data not Writing to Database, SuiteCRM, Custom Import Script

我正在为 SuiteCRM 编写自定义导入脚本,但出现错误:

Warning: array_combine() expects parameter 2 to be array, boolean given in /var/www/html/afscmedbv5test/custom/wimporter/newimporter.php on line 162

我的脚本如下:

<?php
if (!defined('sugarEntry') || !sugarEntry) die ('Not a Valid Entry Point!');

$date = new DateTime();
echo '<H2>Wilderness Import Started</h2>';
echo $date->format('r').'<br>';
echo '----------------------------------------------------------------------- 
--------------------------------------------------------------<br>';

require_once("include/utils/sugar_file_utils.php");

WildernessImportJob();
die();

function var_dump_ret($mixed = null) {
ob_start();
var_dump($mixed);
$content = ob_get_contents();
ob_end_clean();
return $content;
}

function time_elapsed()
{
static $first = null;
static $previous = null;
$now = microtime(true);
if ($first == null) $first = $now;
if ($previous != null)
echo '--- Partial ' . round(($now - $previous), 2) . ', Total ' . round(($now 
- $first), 2) . ' ---';  // 109s
$ret = round(($now - $previous), 2);
$previous = $now;
return $ret;
}
function myLog ($str2log)
{
file_put_contents('./zlog_'.date("j.n.Y").'.txt', date("H:i:s", time())." 
".$str2log.PHP_EOL, FILE_APPEND);
}
function calcDelta($a1, $a2)
{
//combine into a nice associative array:
$delta=Array();
foreach ($a1 as $key=>$value)
{
    if ($a1[$key] != $a2->$key)
    $delta[] = array($key => ("Was ". $a1[$key]. ", became " . $a2->$key));
}
$num = count($data);
if (empty($a1)) $delta[] = array("a1" => ("Was empty"));
if (empty($a2)) $delta[] = array("a2" => ("Was empty"));
return $delta;
}
require_once("include/utils/sugar_file_utils.php");


function fillPerson($record, &$person)
{

// $record is what is being imported from CSV
// $person is the bean about to be filled and going into the SuitCRM DB. It 
may be new or not, depending on whether it exists in the DB previously.
// name: only updates if not existant yet, because it's the key we use for 
search, and because names are more complex with parts


if ($person->full_name_c == "") {
    $recordname = $record["FULL NAME"]; // != "") ? $record["FULL NAME"] : " 
[To-be-filled]");
    //echo $prefix;
    $recordname = str_replace("  ", " ", $recordname);
    echo $recordname;
    $parts = explode(" ", $recordname);
    $person->last_name = array_pop($parts);
    $person->first_name = $parts[0];
    $person->name = $person->first_name . " " . $person->last_name;
    $person->full_name_c = $record["FULL NAME"]; // custom field created in 
    Studio
}
//$datanasc = DateTime::createFromFormat('!m/d/Y', $record["PPE"]);
//  $datasnasc->setTime(0, 0);

//  $person->ppe_date_c = ($datanasc == false) ? "" : $datanasc->format('m-d- 
Y');
//$person->ppe_date_c = $record["PPE"];
//print_r($person);
var_dump($person);
}

// finish by making a complete analysis of what changed:
return calcDelta($person->fetched_row, $person);


function GetOrCreateMember ($the_name)
{
//Check if the fullname is null
if ($the_name != "")
{
$person = BeanFactory::getBean("locte_Members");
$person = $person->retrieve_by_string_fields(array('full_name_c' => 
$the_name));
if (is_null($person))
{
  //get members bean
  $person = BeanFactory::newBean("locte_Members");
  //set full_name_c to the_name variable
  $person->full_name_c = $the_name;
//  $person->lcl_employee_id = $personEmployeeID;
  $person_name = str_replace("  ", " ", $the_name);
  $parts = explode(" ", $person_name);
  $person->last_name = array_pop($parts);
  $person->first_name = $parts[0];
  //combine first and last name to populate the fullname field
  $person->name = $person->first_name . " " . $person->last_name;
  $person_id = $person->save();
  // add new duespayment to member record
//  $rosterDuesPayments = BeanFactory::getBean('Dues Payments')- 
>retrieve_by_string_fields(array('name'=> $duesEmployeeID));
//  $person->load_relationship('locte_Members_adues_dues'); //confirm 
relationship name in cache
//  $person->dues_payments->add($rosterDuesPayments->id);
}
return $person;
}
return null;
}
function WildernessImportJob()
{
try
{
    time_elapsed();
    $GLOBALS['log']->info('Wilderness Import');
    $config = new Configurator();
    $config->loadConfig();
    $xmlDataDir =  'custom/wimporter/ToImport';      //$config->config['WildernessImporter_DataFilePath'];
    $GLOBALS['log']->info("Wilderness Import: Scanning XML Data dir $xmlDataDir...");
        echo("<h3>Wilderness Import: Scanning XML Data dir $xmlDataDir...<br /></h3>");
    $directoryContent = scandir($xmlDataDir);
    $GLOBALS['log']->info("Wilderness Import: Scanning XML Data dir $xmlDataDir... [Found " . count($directoryContent) . " files]");
        echo("<h3>Wilderness Import: Scanning XML Data dir $xmlDataDir... [Found " . count($directoryContent) . " files]</h3><br />");
      foreach ($directoryContent as $itemFile)
    {
          if (is_dir($xmlDataDir . DIRECTORY_SEPARATOR . $itemFile)) continue;
          if (strcasecmp(substr($itemFile, -4), ".csv") != 0) continue;
          $GLOBALS['log']->info("Wilderness Import: Processing $itemFile file...");
          myLog("---------------------------------------------------");
          myLog("Wilderness Import: Processing $itemFile file...");
          myLog("----------------------------------------------------");
                echo("<h4>---------------------------------------------------------------</h4>");
                echo("<h4>Wilderness Import: Processing $itemFile file...</h4>");
          echo("<h4>---------------------------------------------------------------</h4>");
          $oFile = fopen($xmlDataDir . DIRECTORY_SEPARATOR . $itemFile, 'r');
          if ($oFile !== FALSE)
      {
              // read entire file at once:
                  // expected separator is ",", expected encoding is UTF-8 without BOM (BOM is 3 weird characters in beginning of file)
                     while (($data[] = fgetcsv($oFile, 0, ',')) !== FALSE) { }
                                 echo('File opened..... <br /> <br />');
                        fclose($oFile);
                           //combine into a nice associative array:
                        $arow=Array();
                                        echo('Building CSV File Row Array <br /><br />');
                        $fields = array_shift($data);
                                        echo('Building CSV Header Fields Array as shown below:<strong> <br /><br />');
                                        echo implode(", ", $fields) . "</strong><br /><br />\n";


                          foreach ($data as $i=>$arow)

                    {
                    $GLOBALS['log']->info("Wilderness Import: array_combine " . $i);
                            $data[$i] = array_combine($fields, $arow);

                          }
                  unset($arow); // **********************************************! ! ! !! ! ! ! ! ! !! !


                  $num = count($data);
                                        echo('Build Full Array of Roster to be Imported Complete.  Entries to be imported are shown below <br /><br />');


                            for ($row=0; $row < $num - 1 ; $row++)
                            {   // normal bounds: from 0 to $num


                                  //$num is the number of lines including header in csv file
                                  echo "<strong>Filename: $itemFile   |   Roster Import, Row" . ($row + 1) . ":</strong><br />\n";
                                  $GLOBALS['log']->info("Wilderness Import: Importing " . $data[$row]["FULL NAME"]);
                                //  echo("<strong>Importing Roster Row #: ". ($row + 1) . "<br />" . "Local Number " . $data[$row]["AFFILIATE"] . "<br />" . "Employee: " . $data[$row]["FULL NAME"] . "</strong><br /><br />");

                                                echo "<strong><table>\n";
                                foreach ($fields as $field) {
                                //echo "<tr><td>" . $field . "</td><td>" . $data[$row][$field] . "</td><td>" . $data[$row+1][$field] . "</td><td>" . $data[$row+2][$field] . "</td></tr>\n";
                                }
                                echo "</table>\n";
                                                                echo "File Row Data:      ";
                                echo implode(", ", $data[$row]) . "</strong><br /><br />\n";


                                                                $Member = BeanFactory::getBean("locte_Members");
                                                                $FullName=$Member->full_name_c;
                                                              //$myfield_defs = $Member->getFieldDefinitions(); // just to help while developing
                                                                //foreach($myfield_defs as $def) echo $def["name"] . "<br />\n";


                              $Member=$Member->retrieve_by_string_fields(array('full_name_c' => $data[$row]["FULL NAME"]));



                                if (is_null($Member)) {
                                $Member = BeanFactory::newBean("locte_Members");
                                    $delta = fillPerson($data[$row], $Member, "");    //->full_name_c, "FULL NAME");
                        }
                                                    if (count($delta)) {
                                                        $Member_id = $Member->save();
                                    }
      }
              // Records have been saved: from this point on, only work on relationships:
        $GLOBALS['log']->info('End: Wilderness Import');
        myLog('End: Wilderness Import');
        time_elapsed();
        return true;
            }
        }
} catch (Exception $e)
{
    $GLOBALS['log']->fatal("Wilderness Import: Exception " . $e->getMessage());
    myLog("Wilderness Import: Exception " . $e->getMessage());
    echo '\n\nCaught exception: ',  $e->getMessage(), "\n";
  return false;
}
}

它从数据库和 csv 文件获取 return 信息。

错误图片如下。

Error - Capture from browser

帮助总是感激 :)

此脚本抛出错误,因为提供给 array_combine 的参数 $arow 不是数组。所以应该有一个检查来检查 $arow 是否是一个数组。 尝试以下代码:

foreach ($data as $i => $arow) {
    $GLOBALS['log']->info("Wilderness Import: array_combine " . $i);
    if (is_array($arow)) {
        $data[$i] = array_combine($fields, $arow);
    }
}

阅读更多关于 array_combine

更新

您用来读取 csv 的代码需要更改。 fgetcsv 中的第二个参数必须大于要在 CSV 文件中找到的最长行(以字符为单位)。所以替换代码

while (($data[] = fgetcsv($oFile, 0, ', ')) !== FALSE) {

}

while (($data[] = fgetcsv($oFile, 10, ', ')) !== FALSE) {

}

阅读更多关于 fgetcsv

这个块正在使用 beanfactory 保存记录....我认为:|

$Member = BeanFactory::getBean("locte_Members");
                                                                $FullName=$Member->full_name_c;
                                                              //$myfield_defs = $Member->getFieldDefinitions(); // just to help while developing
                                                                //foreach($myfield_defs as $def) echo $def["name"] . "<br />\n";
                              $Member=$Member->retrieve_by_string_fields(array('full_name_c' => $data[$row]["FULL NAME"]));
                                if (is_null($Member)) {
                                $Member = BeanFactory::newBean("locte_Members");
                                    $delta = fillPerson($data[$row], $Member->full_name_c, "FULL NAME");
                                                                var_dump($arow);
                        }
                                                    if (count($delta)) {
                                                        $Member_id = $Member->save();
                                    }
      }
              // Records have been saved: from this point on, only work on relationships:

这是带有字段定义转储的浏览器中脚本的输出

荒野导入开始

2018 年 6 月 19 日,星期二 02:05:56 -0400

Wilderness 导入:正在扫描 XML 数据目录 custom/wimporter/ToImport... 荒野导入:正在扫描 XML 数据目录 custom/wimporter/ToImport... [找到 3 个文件]


Wilderness 导入:正在处理 L1554v4.csv 文件...

文件已打开.....

构建 CSV 文件行数组

构建 CSV Header 如下所示的字段数组:

姓氏、名字、全名

建立完整的待导入名册阵列。要导入的条目如下所示

文件名:L1554v4.csv |花名册导入,第 1 行: 姓巴特勒
名字 BRIANA
全名 BUTLER BRIANA
文件行数据:BUTLER、BRIANA、BUTLER BRIANA

-id

-姓名

-date_entered

-date_modified

-modified_user_id

-modified_by_name

-created_by

-created_by_姓名

-描述

-已删除

-安全组

-securitygroup_display

-created_by_link

-modified_user_link

-assigned_user_id

-assigned_user_name

-assigned_user_link

-其他用户

-additionalusers_listview

-称呼

-first_name

-last_name

-full_name

-标题

-照片

-部门

-do_not_call

-phone_home

-电子邮件

-phone_mobile

-phone_work

-phone_other

-phone_fax

-email1

-邮箱2

-invalid_email

-email_opt_out

-primary_address_street

-primary_address_street_2

-primary_address_street_3

-primary_address_city

-primary_address_state

-primary_address_postalcode

-primary_address_country

-alt_address_street

-alt_address_street_2

-alt_address_street_3

-alt_address_city

-alt_address_state

-alt_address_postalcode

-alt_address_country

-助理

-assistant_phone

-email_addresses_primary

-email_addresses

-email_addresses_non_primary

-lcl_birthdate

-lcl_affiliate_number

-member_signature

-ssn

-staff_rep

-mem_join_date

-class_title

-管家

-olo_code

-enterprise_id

-member_card

-address_map

-member_status

-primary_language

-english_speaking

-annual_salary

-currency_id

-state_hire_date

-dues_frequency

-card_sent_date

-性别

-残疾

-marital_status

-executive_board

-affiliate_type

-middle_name

-name_suffix

-可邮寄标志

-no_mailflag

-apt_number

-zip4

-infosrc

-lcl_phone_collected

-lcl_hasmobile

-lcl_hasemail

-lcl_member_status

-后台助理

-backoffice_assistant_phonoe

-backoffice_assistant_email

-lcl_employercontact

-lcl_work_dept

-lcl_work_location

-lcl_jobclasstitle

-lcl_employee_id

-lcl_join_date

-lcl_sig_auth

-lcl_dues_status

-lcl_on_probation

-lcl_prob_end_date

-lcl_prob_term

-afs_signed_by

-afs_region_number

-afemp_employers_id_c

-afs_employer

-primary_address_county

-locte_members_adues_dues

-full_name_c

-locte_members_afcbu_cbu

-locte_members_afcbu_cbu_姓名

-locte_members_afcbu_cbuafcbu_cbu_idb

-ppe_date_c

---部分0.01,总计0.01---