我尝试使用CodeIgniter 3和PortPHP库将csv导入数据库。
https://github.com/portphp/portphp
这是我的函数输入:
function input_csv() {
// Create and configure the reader
$file = new \SplFileObject('input.csv');
$csvReader = new CsvReader($file);
// Tell the reader that the first row in the CSV file contains column headers
$csvReader->setHeaderRowNumber(0);
// Create the workflow from the reader
$workflow = new StepAggregator($csvReader);
// Create a writer: you need Doctrine’s EntityManager.
$doctrineWriter = new DoctrineWriter($entityManager, 'MyApp:Event');
$workflow->addWriter($doctrineWriter);
// Add a converter to the workflow that will convert `beginDate` and `endDate`
// to \DateTime objects
$dateTimeConverter = new DateTimeValueConverter('Ymd');
$converterStep = new ValueConverterStep();
$converterStep
->add('beginDate', $dateTimeConverter)
->add('endDate', $dateTimeConverter);
$workflow->addStep($converterStep);
// Process the workflow
$workflow->process();
}
观
<form method="post" action="<?= site_url('home/read_csv') ?>">
<div class="modal-footer">
<button class="btn btn-secondary" data-dismiss="modal">Close</button>
<button class="btn btn-primary">Submit</button>
</div>
</form>
我的数据库表有名称:表事务
1.我怎样才能确定这个应该输入到哪个表?
1.当我提交表单时,我得到:
Severity: Notice
Message: Undefined variable: entityManager
Filename: controllers/Home.php
Line Number: 55```
对于Q1,我有代码:
$spreadsheet = $reader->load($_FILES['uploadFile']['tmp_name']);
$sheet_data = $spreadsheet->getActiveSheet(0)->toArray();
$array_data = [];
for($i = 1; $i < count($sheet_data); $i++) {
$data = array(
'name' => $sheet_data[$i]['0'],
'price' => $sheet_data[$i]['1'],
'qty' => $sheet_data[$i]['2'],
'input_date' => $sheet_data[$i]['3']
);
$array_data[] = $data;
}
if($array_data != '') {
$this->home_model->insert_transaction_batch($array_data);
}
$this->modal_feedback('success', 'Success', 'Data Imported', 'OK');
} else {
$this->modal_feedback('error', 'Error', 'Import failed', 'Try again');
}
redirect('/');
但是有人能帮我实现上面的代码吗
@更新完整代码
function input_csv() {
// Create and configure the reader
$file = new \SplFileObject('input.csv');
$csvReader = new CsvReader($file);
// Tell the reader that the first row in the CSV file contains column headers
$csvReader->setHeaderRowNumber(0);
// Create the workflow from the reader
$workflow = new StepAggregator($csvReader);
// Create a writer: you need Doctrine’s EntityManager.
$doctrineWriter = new DoctrineWriter($entityManager, 'tbl_transaction');
$workflow->addWriter($doctrineWriter);
$this->load->library('doctrine');
$entityManager = $this->doctrine->em;
$csvReader = new CsvReader($file, $entityManager);
// Add a converter to the workflow that will convert `beginDate` and `endDate`
// to \DateTime objects
$dateTimeConverter = new DateTimeValueConverter('Ymd');
$converterStep = new ValueConverterStep();
$converterStep
->add('beginDate', $dateTimeConverter)
->add('endDate', $dateTimeConverter);
$workflow->addStep($converterStep);
$spreadsheet = $reader->load($_FILES['uploadFile']['tmp_name']);
$sheet_data = $spreadsheet->getActiveSheet(0)->toArray();
$array_data = [];
for($i = 1; $i < count($sheet_data); $i++) {
$data = array(
'name' => $sheet_data[$i]['0'],
'price' => $sheet_data[$i]['1'],
'qty' => $sheet_data[$i]['2'],
'input_date' => $sheet_data[$i]['3']
);
$array_data[] = $data;
}
if($array_data != '') {
$this->home_model->insert_transaction_batch($array_data);
}
$this->modal_feedback('success', 'Success', 'Data Imported', 'OK');
} else {
$this->modal_feedback('error', 'Error', 'Import failed', 'Try again');
}
redirect('/');
// Process the workflow
$workflow->process();
}
1条答案
按热度按时间ijnw1ujt1#
您的问题摘要:
1.我如何指定应该输入到哪个表?
1.提交表单时出现错误...
1.但是有谁能帮我实现上面的代码到我们的代码。
对于Q1,可以在DoctrineWriter构造函数中指定表名称:
对于Q2,您需要将实体管理器传递给控制器:
然后你可以把它传给读者:
对于Q3,可以使用以下代码: