I have one problem with importing data from csv to database.
For now, my code looks like this:
public function run()
{
$this->startProgressBar();
foreach ($this->elements as $element) {
$this->insertCity($element);
$this->advanceProgressBar();
}
$this->finishProgressBar();
}
/**
* @param array $item
*/
private function insertCity(array $item = [])
{
$repository = $this->getDoctrine()->getRepository(Commune::class);
$commune = $repository->findOneByTerc($this->getTerc($item));
$district = $item['uid'] == $item['district_uid'] ? null : $item['district_uid'];
$city = new City();
$city->setName($item['name']);
$city->setCommuneId($commune->getId());
$city->setDistrictUid($district);
$city->setType($item['city_type']);
$city->setUid($item['uid']);
$this->getDoctrine()->getManager()->persist($city);
$this->getDoctrine()->getManager()->flush();
}
Every one row I make select and insert. My csv file has 100k rows. In 1 hour, this code imports only 10k rows :(
Any ideas, how can I optimize it?
Filip.