Symfony 表单从 CSV 文件中检索数据并将其注入数据库
Symfony form to retrieve data from a CSV file and inject it into the database
我不明白如何使用 FileType 表单从 csv 中检索数据并将其注入数据库。
使用普通表单,我可以毫无问题地检索数据。
//Controller.php
$data = new Data();
$form = $this->createFormBuilder($data)
->add('name')
->add('Nbmessage', IntegerType::class)
->add('status')
->add('details')
->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
$em->persist($data);
$em->flush();
}
---------------------------------------------------------------
// import.html.twig
{{ form_start(form)}}
{{ form_widget(form) }}
<button type="submit" class="btn btn-success">Add</button>
{{ form_end(form)}}
但我希望使用 Symfony 表单将此数据格式置于 csv 中。
这是我目前的位置,经过多次尝试
$form2 = $this->createFormBuilder()
->add('submitFile', FileType::class)
->getForm();
if ($request->getMethod('post') == 'POST') {
$form2->handleRequest($request);
if ($form2->isSubmitted() && $form2->isValid()) {
$file = $form2->get('submitFile');
$file->getData();
}
}
找了一个星期了。。。实在是不懂怎么弄。
在 FileType
上调用 getData()
将 return 一个 UploadedFile
实例。这表示传输后服务器中的文件。然后你必须按照你认为合适的方式处理它:如果它是一个简单的上传,则将它移动到一个文件夹,或者像你的情况一样,做一些进一步的处理。
您可能决定使用库而不是内置函数,但希望这会帮助您上路:
function upload(Request $request, EntityManager $em)
{
// Form creation ommited
$form2->handleRequest($request);
if ($form2->isSubmitted() && $form2->isValid()) {
/** @var UploadedFile */
$file = $form2->get('submitFile')->getData();
// Open the file
if (($handle = fopen($file->getPathname(), "r")) !== false) {
// Read and process the lines.
// Skip the first line if the file includes a header
while (($data = fgetcsv($handle)) !== false) {
// Do the processing: Map line to entity, validate if needed
$entity = new Entity();
// Assign fields
$entity->setField1($data[0]);
$em->persist($entity);
}
fclose($handle);
$em->flush();
}
}
}
我不明白如何使用 FileType 表单从 csv 中检索数据并将其注入数据库。
使用普通表单,我可以毫无问题地检索数据。
//Controller.php
$data = new Data();
$form = $this->createFormBuilder($data)
->add('name')
->add('Nbmessage', IntegerType::class)
->add('status')
->add('details')
->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
$em->persist($data);
$em->flush();
}
---------------------------------------------------------------
// import.html.twig
{{ form_start(form)}}
{{ form_widget(form) }}
<button type="submit" class="btn btn-success">Add</button>
{{ form_end(form)}}
但我希望使用 Symfony 表单将此数据格式置于 csv 中。
这是我目前的位置,经过多次尝试
$form2 = $this->createFormBuilder()
->add('submitFile', FileType::class)
->getForm();
if ($request->getMethod('post') == 'POST') {
$form2->handleRequest($request);
if ($form2->isSubmitted() && $form2->isValid()) {
$file = $form2->get('submitFile');
$file->getData();
}
}
找了一个星期了。。。实在是不懂怎么弄。
在 FileType
上调用 getData()
将 return 一个 UploadedFile
实例。这表示传输后服务器中的文件。然后你必须按照你认为合适的方式处理它:如果它是一个简单的上传,则将它移动到一个文件夹,或者像你的情况一样,做一些进一步的处理。
您可能决定使用库而不是内置函数,但希望这会帮助您上路:
function upload(Request $request, EntityManager $em)
{
// Form creation ommited
$form2->handleRequest($request);
if ($form2->isSubmitted() && $form2->isValid()) {
/** @var UploadedFile */
$file = $form2->get('submitFile')->getData();
// Open the file
if (($handle = fopen($file->getPathname(), "r")) !== false) {
// Read and process the lines.
// Skip the first line if the file includes a header
while (($data = fgetcsv($handle)) !== false) {
// Do the processing: Map line to entity, validate if needed
$entity = new Entity();
// Assign fields
$entity->setField1($data[0]);
$em->persist($entity);
}
fclose($handle);
$em->flush();
}
}
}