-1

我目前正在处理 .csv 导入,它导入议程项目(例如事件)并将它们放在 MySQL 数据库中。我已经掌握了它的要点,但是有一段很麻烦的代码,我似乎无法解决如何修复它或使它更高效/更好。

对我来说很麻烦的代码部分是每次举办一个活动 - 让我们说tuesday-sunday2012-06-01'直到2013-04-01并且您想检查其中一些值是否已在数据库中准备好(因为用户可能会使用一些调整后的值再次导入 .csv 文件)。我当前的代码最终在数据库中实现了正确的值,但它的完成方式有点麻烦。如果文件再次被导入,所有来自星期天的值都会再次被删除,因为我的检查首先在星期二通过 foreach,它将检查数据库值是否与星期二生成的值匹配。检查完成并将值插入数据库后,我的 foreach 会检查星期天的所有值(星期二不会被删除,因为我保留了一个包含所有先前插入值的数组),然后再次插入它们。所以基本上当我导入一个 .csv 文件时,它会删除然后插入相同的值,我的问题是如何防止这种情况发生?

此外,如果您发现有任何改进此代码的方法(因为我真的觉得一开始就效率不高)请说出来,非常感谢。

这是检查事件重复的代码,如果您需要任何说明或其他代码,请告诉我:

//REPEAT DAYS OF THE WEEK
if(!empty($repeatDayOfTheWeek) && $repeatDayOfTheWeek != '')
{
    $daysOfTheWeek = explode(',', $repeatDayOfTheWeek);

    foreach($daysOfTheWeek as $key => $dayOfTheWeek)
    {
        if(!is_numeric($dayOfTheWeek))
        {
            continue;
        }

        unset($agendaI->agendaItemValues['stopDate']);

        $beginDate = strtotime($tempStartDate);
        $endDate = strtotime($tempStopDate);

        $dayDates = array();
        $arrayDatesInDb = array();

        if(!isset($previousInserts))
        {
            $previousInserts = array();
        }

        if($beginDate != '' && $endDate != '')
        {
            while($beginDate <= $endDate)
            {
                if(date('N', $beginDate) == $dayOfTheWeek)
                {
                    $dayDates[] = date('Y-m-d', $beginDate);
                }

                $beginDate = strtotime("+1 day", $beginDate);
            }

        $datesInDb = $agendaI->getAgendaItemsByExternId($externId);

        if(empty($datesInDb))
        {
            foreach($dayDates as $dayDate)
            {
                dump("Empty DB - INSERT: ".$dayDate);
                $agendaI->setAgendaItemValue('startDate', $dayDate);
                $agendaI->saveAgendaItem();
                $previousInserts[] = $dayDate;
            }
        }
        else
        {
            dump('DB with records');
            foreach($datesInDb as $dateInDb)
            {
                if(!in_array($dateInDb->startDate, $dayDates) && !in_array($dateInDb->startDate, $previousInserts))
                {
                    dump("Not in Item-array, but in DB - DELETE: ".$dateInDb->startDate);
                    $agendaI->deleteAgendaItem($dateInDb->id);
                }
            }

        foreach($dayDates as $dayDate)
        {
            foreach($datesInDb as $dateInDb)
            {
                if($dayDate == $dateInDb->startDate)
                {
                    $arrayDatesInDb[] = $dateInDb->startDate;
                    dump("In array & in DB - UPDATE: ".$dateInDb->startDate);
                    $agendaI->setAgendaItemValue('id', $dateInDb->id);
                    $agendaI->saveAgendaItem();
                    $previousInserts[] = $dayDate;
                }
            }
        }

    unset($agendaI->agendaItemValues['id']);

    foreach($dayDates as $dayDate)
    {
        if(!in_array($dayDate, $arrayDatesInDb))
        {
            dump("Not in DB-array but in Item-array - INSERT: ".$dayDate);
            $agendaI->setAgendaItemValue('startDate', $dayDate);
            $agendaI->saveAgendaItem();
            $previousInserts[] = $dayDate;
        }
    }
}

以下字段位于 .csv 中:

"Datum" "Tijdstip" "Evenement" "Locatie" "Website" "Toelichting" "Duits" "engels" "startdatum" "stopdatum" "herhalen dag van de week" "externid" "categorie" "via"

换句话说
,我需要检查数据库中的记录是否与 .csv 中的记录相同。如果 csv 没有数据库的记录,我需要从数据库中删除它(我当前的代码确实如此,只是效率不高)

4

2 回答 2

1

您可以在,和列之间定义UNIQUE索引,而不是在 PHP 中实现检查;然后使用您的命令将与这些字段匹配的任何现有记录替换为新记录:startdatumstopdatumherhalen dag van de weekREPLACELOAD DATA

ALTER TABLE events ADD UNIQUE INDEX (startdatum, stopdatum, dag);

LOAD DATA INFILE '/path/to/foo.csv'
  REPLACE
  INTO TABLE events;
于 2012-06-06T09:38:47.330 回答
0

I eventually solved it in the following way without using a foreach for each $daysoftheweek and using array_diff:

            //REPEAT DAYS OF THE WEEK
            if(!empty($repeatDayOfTheWeek) && $repeatDayOfTheWeek != '')
            {
                $daysOfTheWeek = explode(',', $repeatDayOfTheWeek);

                $agendaI->setAgendaItemValue('stopDate', '');

                $beginDate = strtotime($startDate);
                $endDate = strtotime($stopDate);
                $dayDates = array();
                $dbValues = array();
                $dbValues['datum'] = array();
                $latestDbValues = array();
                $toBeDeletedKeys = array();

                while($beginDate <= $endDate)
                {
                    if(in_array(date('N', $beginDate), $daysOfTheWeek))
                    {
                        $dayDates[] = date('Y-m-d', $beginDate);
                    }

                    $beginDate = strtotime("+1 day", $beginDate);
                }

                $evenementenInDb = $agendaI->getAgendaItemsByExternId($externId);

                if(!empty($evenementenInDb))
                {
                    foreach($evenementenInDb as $evenementInDb)
                    {
                        $dbValues['agendaid'][] = $evenementInDb->id;
                        $dbValues['datum'][] = $evenementInDb->startDate;
                    }
                }

                foreach($dayDates as $dayDate)
                {
                    $key = array_search($dayDate, $dbValues['datum']); 
                    if($key !== false && is_numeric($key))
                    {
                        //UPDATE
                        $agendaI->setAgendaItemValue('id', $dbValues['agendaid'][$key]);
                        $agendaI->setAgendaItemValue('startDate', $dayDate);
                        $agendaI->saveAgendaItem();
                        $latestDbValues[] = $dayDate;
                        dump('UPDATE');
                    }
                    else
                    {
                        //INSERT
                        unset($agendaI->agendaItemValues['id']);
                        $agendaI->setAgendaItemValue('startDate', $dayDate);
                        $agendaI->saveAgendaItem();
                        $latestDbValues[] = $dayDate;
                        dump('INSERT');
                    }
                }

                $toBeDeleted = array_diff($dbValues['datum'], $latestDbValues);
                foreach($toBeDeleted as $value)
                {
                    $toBeDeletedKeys[] = array_search($value, $dbValues['datum']);
                }

                foreach($toBeDeletedKeys as $toBeDeletedKey)
                {
                    //DELETE
                    $agendaI->deleteAgendaItem($dbValues['agendaid'][$toBeDeletedKey]);
                    dump('DELETE');
                }
            }
            else 
            { 
                //Normal, non repeating event
                //INSERT
                if(!empty($agendaItem) && is_numeric($agendaItem->id))
                {
                    $agendaI->setAgendaItemValue('id',$agendaItem->id);
                }

                $agendaId = $agendaI->saveAgendaItem();
            }
于 2012-06-07T13:17:45.447 回答