Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Import des villes depuis EUCircos
<?php
namespace AppBundle\Command;
use AppBundle\Entity\Departement;
use AppBundle\Entity\Region;
use AppBundle\Entity\Ville;
use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class AppCitiesCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('app:cities')
->setDescription('...')
->addArgument('argument', InputArgument::OPTIONAL, 'Argument description')
->addOption('option', null, InputOption::VALUE_NONE, 'Option description')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
/* @var $em EntityManager */
$em = $this->getContainer()->get('doctrine')->getManager();
// yolo
ini_set("memory_limit", "-1");
// On vide les 3 tables
$connection = $em->getConnection();
$platform = $connection->getDatabasePlatform();
$connection->executeUpdate($platform->getTruncateTableSQL('ville', true /* whether to cascade */));
$connection->executeUpdate($platform->getTruncateTableSQL('region', true /* whether to cascade */));
$connection->executeUpdate($platform->getTruncateTableSQL('departement', true /* whether to cascade */));
$csv = dirname($this->getContainer()->get('kernel')->getRootDir()) . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . 'villes.csv';
$lines = explode("\n", file_get_contents($csv));
$regions = [];
$departements = [];
$villes = [];
foreach ($lines as $k => $line) {
$line = explode(';', $line);
if (count($line) > 10 && $k > 0) {
// On sauvegarde la region
if (!key_exists($line[1], $regions)) {
$region = new Region();
$region->setCode($line[1]);
$region->setName($line[2]);
$regions[$line[1]] = $region;
$em->persist($region);
} else {
$region = $regions[$line[1]];
}
// On sauvegarde le departement
if (!key_exists($line[4], $departements)) {
$departement = new Departement();
$departement->setName($line[5]);
$departement->setCode($line[4]);
$departement->setRegion($region);
$departements[$line[4]] = $departement;
$em->persist($departement);
} else {
$departement = $departements[$line[4]];
}
// On sauvegarde la ville
$ville = new Ville();
$ville->setName($line[8]);
$ville->setCode($line[9]);
$ville->setDepartement($departement);
$villes[] = $line[8];
$em->persist($ville);
}
}
$em->flush();
$output->writeln(count($villes) . ' villes importées');
}
}
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Region
*
* @ORM\Table(name="departement")
* @ORM\Entity(repositoryClass="AppBundle\Repository\RegionRepository")
*/
class Departement
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var Region
*
* @ORM\ManyToOne(targetEntity="Region")
*/
private $region;
/**
* @var string
*
* @ORM\Column(name="code", type="integer", nullable=true)
*/
private $code;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Region
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set code
*
* @param integer $code
*
* @return Departement
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* Get code
*
* @return integer
*/
public function getCode()
{
return $this->code;
}
/**
* @return Region
*/
public function getRegion(): Region
{
return $this->region;
}
/**
* @param Region $region
*/
public function setRegion(Region $region)
{
$this->region = $region;
}
}
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Region
*
* @ORM\Table(name="region")
* @ORM\Entity(repositoryClass="AppBundle\Repository\RegionRepository")
*/
class Region
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="code", type="integer", nullable=true)
*/
private $code;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Region
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set code
*
* @param integer $code
*
* @return Region
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* Get code
*
* @return integer
*/
public function getCode()
{
return $this->code;
}
}
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Region
*
* @ORM\Table(name="ville")
* @ORM\Entity(repositoryClass="AppBundle\Repository\RegionRepository")
*/
class Ville
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var Departement
*
* @ORM\ManyToOne(targetEntity="Departement")
*/
private $departement;
/**
* @var string
*
* @ORM\Column(name="code", type="integer", nullable=true)
*/
private $code;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Region
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set code
*
* @param integer $code
*
* @return Ville
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* Get code
*
* @return integer
*/
public function getCode()
{
return $this->code;
}
/**
* @return Departement
*/
public function getDepartement(): Departement
{
return $this->departement;
}
/**
* @param Departement $departement
*/
public function setDepartement(Departement $departement)
{
$this->departement = $departement;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.