public function regionsAction() { die('nope'); // http://ws.geonames.org/search?q=&country=HR&fclass=A&username=nebs&fcode=ADM1 $em = $this->getDoctrine()->getManager(); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://ws.geonames.org/searchJSON?q=&country=HR&fclass=A&username=nebs&fcode=ADM1&maxRows=1000'); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json')); // Assuming you're requesting JSON curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); // If using JSON... $data = json_decode($response); foreach ($data->geonames as $region) { $country = $em->getRepository('AppBundle:Country')->findOneByName($region->countryName); $name = $region->name; $geonameId = $region->geonameId; $regionObj = new Region(); $regionObj->setName($name); $regionObj->setGeonameId($geonameId); $regionObj->setCountry($country); $em->persist($regionObj); $em->flush(); } return $this->render('AdminBundle::parse.html.twig', array('data' => $data->geonames)); }
protected function execute(InputInterface $input, OutputInterface $output) { $io = new SymfonyStyle($input, $output); $em = $this->getContainer()->get('doctrine.orm.entity_manager'); $file = $input->getArgument('file'); $regions = json_decode(file_get_contents($file)); foreach ($regions as $region) { $reg = new Region(); $reg->setName($region->name); $reg->setWsId($region->id); $reg->setType($region->type); foreach ($region->tournaments as $tournament) { $tour = new Tournament(); $tour->setRegion($reg); $tour->setWsId($tournament->id); $tour->setName($tournament->name); $em->persist($tour); } $em->persist($reg); } $em->flush(); $io->success('Added regions and tournaments.'); }
public function getRegions() { $regions = json_decode(file_get_contents(__DIR__ . "/../Resources/regions.json")); foreach ($regions as $region) { $reg = new Region(); $reg->setName($region->name); $reg->setWsId($region->id); $reg->setType($region->type); foreach ($region->tournaments as $tournament) { $tour = new Tournament(); $tour->setRegion($reg); $tour->setWsId($tournament->id); $tour->setName($tournament->name); $this->em->persist($tour); } $this->em->persist($reg); } $this->em->flush(); }
/** * @Route("/region/create", name="region_create") */ public function createRegionAction() { $country = $this->getDoctrine()->getRepository('AppBundle:Country')->findOneByName("Ukraine"); $region = new Region(); $region->setName('Kiev area'); $region->setCountry($country); $em = $this->getDoctrine()->getManager(); $em->persist($country); $em->persist($region); $em->flush(); return new Response('Created region id ' . $region->getId()); }
/** * @param OutputInterface $output */ private function getLocations(OutputInterface $output) { $output->writeln('<info>Starting to get Locations</info>'); $output->writeln('<info>URL: ' . $this->url . 'Locations+Overview</info>'); $html = file_get_contents($this->url . 'Locations+Overview'); $crawler = new Crawler($html); /* Get Regions by Faction */ $factionRegions = $crawler->filter('.span_1_of_4'); foreach ($factionRegions as $domElement) { /* @var \DOMElement $domElement */ $content = trim($domElement->textContent); $locations = array_map('trim', explode("\n", $content)); $output->writeln('<info>Getting Factions</info>'); $faction = $this->em->getRepository('AppBundle:Faction')->findOneBy(array('name' => $locations[0])); $output->writeln('<info>Faction: ' . $locations[0] . '</info>'); if (!$faction instanceof Faction) { $faction = new Faction(); $faction->setName($locations[0]); $this->em->persist($faction); } unset($locations[0]); $output->writeln('<info>Getting Regions</info>'); foreach ($locations as $r) { $output->writeln('<info>Region: ' . $r . '</info>'); $region = $this->em->getRepository('AppBundle:Region')->findOneBy(array('name' => $r)); if (!$region instanceof Region) { $region = new Region(); $region->setName($r); $region->setFaction($faction); $this->em->persist($region); } } } $this->em->flush(); /* Get Region by Province */ $provinceRegions = $crawler->filter('table.wiki_table'); $output->writeln('<info>Getting provinces</info>'); $provincesArray = array(); foreach ($provinceRegions->children() as $domElement) { $output->writeln('<info>Making a Province Array</info>'); /* @var \DOMElement $domElement */ $content = trim($domElement->textContent); $contentArray = array_map('trim', explode("\n", $content)); $index = $contentArray[0]; unset($contentArray[0]); if ($index != 'Province') { $provincesArray[$index] = array_map('trim', explode(',', $contentArray[1])); } } $output->writeln('<info>Saving Factions</info>'); foreach ($provincesArray as $prov => $regions) { $output->writeln('<info>' . $prov . '</info>'); $province = $this->em->getRepository('AppBundle:Province')->findOneBy(array('name' => $prov)); if (!$province instanceof Province) { $province = new Province(); $province->setName($prov); $this->em->persist($province); } foreach ($regions as $r) { $output->writeln('<info>' . $r . '</info>'); $region = $this->em->getRepository('AppBundle:Region')->findOneBy(array('name' => $r)); if (!$region instanceof Region) { $region = new Region(); $region->setName($r); } $region->setProvince($province); $this->em->persist($region); } } $this->em->flush(); $output->writeln('<info>We\'re done.</info>'); }