如何处理在Symfony/PHP中导入错误的CSV文件



我正在上传大量数据但是我想保护一个可能的用户错误,从某种意义上说,他们有可能在表单中放入一个错误的csv…

问题是,我截断我的表每次表单启动…


ImportController.php
$form = $this->createFormBuilder()
->add('form', FileType::class, [
'attr' => ['accept' => '.csv',
'class' => 'custom-file-input'],
'label' => 'Import'
])
->getForm();

$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) 
{
/** 
* @var UploadedFile
*/
$file = $form->get('form')->getData();  
$connection = $em->getConnection();
$platform = $connection->getDatabasePlatform();
$connection->beginTransaction();
$connection->executeQuery($platform->getTruncateTableSQL('MyTable', true));
$this->getDoctrine()->getManager()->getRepository(MyTable::class)->importMyData($file);           

$this->addFlash('success',"The csv has been successfully imported");
return $this->redirectToRoute('import');
} 

MyTableRepository.php
public function importMyData($file)
{
$em = $this->entityManager;
if (($handle = fopen($file->getPathname(), "r")) !== false) 
{
$count = 0;
$batchSize = 1000;
$data = fgetcsv($handle, 0, ","); 
while (($data = fgetcsv($handle, 0, ",")) !== false) 
{
$count++;
$entity = new MyTable();
// 40 entity fields...
$entity->setFieldOne($data[0]);                
$entity->setFieldTwo($data[1]); 
//....
$entity->setFieldForty($data[39]); 
$em->persist($entity);
if (($count % $batchSize) === 0 )
{
$em->flush();
$em->clear();
}
}
fclose($handle);
$em->flush();
$em->clear();
}
}

我只是希望当一个错误的CSV文件启动时表不会Truncate

可以在将数据插入数据库之前对其进行消毒和验证。你可以尝试多种方法。首先,我将使用Symfony文件验证器,以确保检查有效的文件已上传。

Symfony File Validator

要验证每一行,您可以使用自定义回调验证器或数组的原始值验证原始数据

//call the validator in the repository
$validator = Validation::createValidator();
// sample input
$input = [
'field_1' => 'hello',
'field_2' => 'test@email.tld',
'field_40' => 3
];
//define the constraints for each row
$constraint = new AssertCollection([
// the keys correspond to the keys in the input array
'field_1' => new AssertCollection([
'first_name' => new AssertLength(['min' => 101]),
'last_name' => new AssertLength(['min' => 1]),
]),
'field_2' => new AssertEmail(),
'field_40' => new AssertLength(['min' => 102])
]);
while (($data = fgetcsv($handle, 0, ",")) !== false) {
$violations = $validator->validate($data, $constraint);

// you can skip the row or log the error
if ($violations->count() > 0) {
continue;
}
}

感谢你们所有人,我很笨,我只是放了一个"try catch"如果在importMyData函数中发生错误,则返回其他东西,然后我检查我的控制器是否返回此值…如果是,我重定向etc.

最新更新