How To Import A CSV in Symfony
In this short series we are covering one way of approaching a question posed by a member here at CodeReviewVideos, which involved importing CSV data into two related tables.
The idea here is that you - as a developer - are given a CSV file by some well-meaning individual, and are tasked with:
- reading the CSV file, row by row
- creating a new entity based on some of the row data
- creating another entity based on other parts of the row data
- relating the two entities
- saving the data off to the database
Along the way, we will see that there are some little unexpected tasks that we would likely want to address, such as improving the speed of the import, and making sure we don't end up with a bunch of duplicates.
The approach covered in this series is "one way". It is not "the way".
You will need to adapt this approach to your own circumstances, and at the various points where I believe it correct to do so, I will offer some advice / guidelines on doing so. Again, this is personal opinion, so use your own best judgement based on your personal circumstances.
By the end of these two videos you will have learned how to:
- read a CSV file from local disk
- create a Symfony console / command line tool for processing the CSV file
- create related Doctrine entities based off the data in your CSV file
- add a progress bar to show ... progress! :)
To complete this tutorial you should have:
- a basic knowledge of Symfony
- a basic knowledge of Doctrine and Entities
- a new or existing working Symfony installation
- database connectivity with read/write access
- a desire to import a CSV file :)
You don't need a CSV file to complete this project. I will cover a way to generate all the CSV data you need as part of the video write ups.