/** * @param ObjectManager $manager * @param string $filename * @return integer Number of entries actually added. */ protected function addEntries(ObjectManager $manager, $filename) { $repo = $this->getRepository($manager); $entriesAdded = 0; $currentBatchEntries = array(); $fcontents = file($filename); for ($i = 0, $numLines = count($fcontents); $i < $numLines; ++$i) { $line = trim($fcontents[$i]); $arr = explode("\t", $line); // skip if no lat/lng values if (!array_key_exists(9, $arr) || !array_key_exists(10, $arr)) { continue; } $country = $arr[0]; $postalCode = $arr[1]; // skip duplicate entries in current batch if (in_array($country . '-' . $postalCode, $currentBatchEntries)) { continue; } // skip duplicate entries already persisted if ($repo->findOneBy(array('country' => $country, 'postalCode' => $postalCode)) !== null) { continue; } $entity = new GeoPostalCode(); $entity->setCountry($country); $entity->setPostalCode($postalCode); $entity->setLat((double) $arr[9]); $entity->setLng((double) $arr[10]); $manager->persist($entity); ++$entriesAdded; $currentBatchEntries[] = $country . '-' . $postalCode; if (($i + 1) % $this->batchSize === 0) { $manager->flush(); $manager->clear(); $currentBatchEntries = array(); echo '.'; // progress indicator } } $manager->flush(); // Flush for the last batch, which doesn't reach the batch size in most cases. (fixes #2) echo ' ', $entriesAdded, "\n"; return $entriesAdded; }
/** * Persists a number of {@code GeoPostalCode}s using dummy data. * @param integer $number */ protected static function persistDummyGeoPostalCodes($number) { $em = static::getEntityManager(); for ($i = 0; $i < $number; ++$i) { $entity = new GeoPostalCode(); $entity->setCountry('DE'); $entity->setPostalCode($i); $entity->setLat('52.' . $i); $entity->setLng('13.' . $i); $em->persist($entity); } $em->flush(); }