【问题标题】:通过 phpspreadsheet 从 Excel 文件导入数据并检查数据库 symfony 4 中是否存在记录
【发布时间】:2020-12-30 01:17:34
【问题描述】:

我会通过phpspreadsheet从Excel文件中导入数据,逐行读取并测试该记录是否存在于数据库中,如下所示

$reader = new XlsxReader();
$spreadsheet = $reader->load($tmp_name);
$worksheet = $spreadsheet->getActiveSheet();
$highestRow = $worksheet->getHighestRow(); // e.g. 10
$highestColumn = $worksheet->getHighestColumn();
fwrite($fp, "File: " . $_FILES['catalogue']['name'] . "\n");
fwrite($fp, "Number of Row : " . $highestRow . "\n");
fwrite($fp, "Highest Column : " . $highestColumn . "\n");
$em = $this->getDoctrine()->getManager()->getRepository(Catalogue::class);
$list = [];

$it = $worksheet->getRowIterator(1);
foreach ($it as $row) {
    $cellIt = $row->getCellIterator();
    $r = [];
    foreach ($cellIt as $cell) {
        $r[] = $cell->getFormattedValue();
    }
    $list[] = $r;
}
foreach ($list as $x) {
    $v = $x[0];
    fwrite($fp, $v . "\n");
    $s = $em->findOneBy(['Species' => $x[0]]);
    if ($s) {
        if ($s->getSpecies() ==$x[0]){
        $request->getSession()->getFlashBag()->add('error', $v . 'Already Existing' . '<br>');
        fwrite($fp, $x[0].'Already Exist in the catalogue'. "\n");
        }
        if ($s->getSpecies() !=$x[0]) {
        $request->getSession()->getFlashBag()->add('sucess', $x[0] . 'To be added' . '<br>');
       fwrite($fp, $x[0]. 'to be added to the catalogue'. "\n");
        }

    }

    //
}
fclose($fp);
$request->getSession()->getFlashBag()->add('success', 'File is valid, and was successfully processed.!' . '<br>' . "<a href=" . $url . ">Log Link</a>");
return $this->redirect($this->generateUrl('Catalogue_list'));

检查该记录是否存在于数据库中只对一半的记录起作用,如下所示:

 - Asparagopsis armata        
 - Abudefduf sexfasciatus
 - Abudefduf sexfasciatus Already Exist in the catalogue    
 - Abudefduf vaigiensis
 - Abudefduf vaigiensis Already Exist in the catalogue
 - Caretta caretta
 - Chelonya mydas

只有前三个记录在数据库中。我做错了什么?

【问题讨论】:

    标签: symfony import doctrine sonata phpspreadsheet


    【解决方案1】:
    $s = $em->findOneBy(['Species' => $x[0]]);
    if ($s) {  // $s is set, so the species exists
        $request->getSession()->getFlashBag()->add('error', $v . 'Already Existing' . '<br>');
        fwrite($fp, $x[0].'Already Exist in the catalogue'. "\n");
    } else {  // Not found
        $request->getSession()->getFlashBag()->add('sucess', $x[0] . 'To be added' . '<br>');
       fwrite($fp, $x[0]. 'to be added to the catalogue'. "\n");
        $catalogue = new Catalogue();
        $catalogue->setSpecies($x[0]);
        $em->persist($s);
        $em->flush();
    }
    
    我收到此错误 传递给 App\Entity\Catalogue::setSpecies() 的参数 1 必须是字符串类型,给定 null,任何帮助

    【讨论】:

    • 添加更多信息edit您的问题或对答案发表评论,而不是在不能解决问题的情况下创建答案。也就是说,是否插入了任何记录?如果是,您可能正在阅读最后一行的数据或有空单元格。尝试使用getHighestDataRow() 而不是getHighestRow()。您也可以print_r($list) 来检查它是否包含您所期望的内容。
    【解决方案2】:

    调用findOneBy 方法只会在物种已经存在于数据库中的情况下返回一些东西,你不需要嵌套的ifs。您应该将第二个(带有!= 条件的那个)移动到else 块,因为现在它永远无法到达:

    $s = $em->findOneBy(['Species' => $x[0]]);
    if ($s) {  // $s is set, so the species exists
        $request->getSession()->getFlashBag()->add('error', $v . 'Already Existing' . '<br>');
        fwrite($fp, $x[0].'Already Exist in the catalogue'. "\n");
    } else {  // Not found
        $request->getSession()->getFlashBag()->add('sucess', $x[0] . 'To be added' . '<br>');
       fwrite($fp, $x[0]. 'to be added to the catalogue'. "\n");
    }
    

    【讨论】:

      猜你喜欢
      • 2019-08-10
      • 1970-01-01
      • 1970-01-01
      • 2013-07-28
      • 2013-08-07
      • 1970-01-01
      • 1970-01-01
      • 2018-02-14
      • 2017-02-13
      相关资源
      最近更新 更多