From 24320cb702df218c44d6d151df748c07966134b8 Mon Sep 17 00:00:00 2001 From: nobohan Date: Wed, 6 Oct 2021 17:28:07 +0200 Subject: [PATCH 1/6] address: alter postalcode entity --- .../ChillMainBundle/Entity/PostalCode.php | 61 +++++++++++++++++++ .../migrations/Version20211006151653.php | 33 ++++++++++ 2 files changed, 94 insertions(+) create mode 100644 src/Bundle/ChillMainBundle/migrations/Version20211006151653.php diff --git a/src/Bundle/ChillMainBundle/Entity/PostalCode.php b/src/Bundle/ChillMainBundle/Entity/PostalCode.php index 910245680..cd7e8aa5f 100644 --- a/src/Bundle/ChillMainBundle/Entity/PostalCode.php +++ b/src/Bundle/ChillMainBundle/Entity/PostalCode.php @@ -2,6 +2,7 @@ namespace Chill\MainBundle\Entity; +use Chill\MainBundle\Doctrine\Model\Point; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Serializer\Annotation\Groups; @@ -62,6 +63,30 @@ class PostalCode */ private $origin = 0; + /** + * @var string + * + * @ORM\Column(type="string", length=255, nullable=true) + * @groups({"write", "read"}) + */ + private $refId; + + /** + * @var string + * + * @ORM\Column(type="string", length=255, nullable=true) + * @groups({"write", "read"}) + */ + private $source; + + /** + * @var Point + * + * @ORM\Column(type="point", nullable=true) + * @groups({"write", "read"}) + */ + private $center; + /** * Get id * @@ -169,5 +194,41 @@ class PostalCode { return $this->country; } + + public function getRefId(): ?string + { + return $this->refId; + } + + public function setRefId(?string $refId): self + { + $this->refId = $refId; + + return $this; + } + + public function getSource(): ?string + { + return $this->source; + } + + public function setSource(?string $source): self + { + $this->source = $source; + + return $this; + } + + public function getCenter(): ?Point + { + return $this->center; + } + + public function setCenter(?Point $center): self + { + $this->center = $center; + + return $this; + } } diff --git a/src/Bundle/ChillMainBundle/migrations/Version20211006151653.php b/src/Bundle/ChillMainBundle/migrations/Version20211006151653.php new file mode 100644 index 000000000..a6a66ce7e --- /dev/null +++ b/src/Bundle/ChillMainBundle/migrations/Version20211006151653.php @@ -0,0 +1,33 @@ +addSql('ALTER TABLE chill_main_postal_code ADD refId VARCHAR(255) DEFAULT NULL'); + $this->addSql('ALTER TABLE chill_main_postal_code ADD source VARCHAR(255) DEFAULT NULL'); + $this->addSql('ALTER TABLE chill_main_postal_code ADD center geometry(POINT,4326) DEFAULT NULL'); + } + + public function down(Schema $schema): void + { + $this->addSql('ALTER TABLE chill_main_postal_code DROP refId'); + $this->addSql('ALTER TABLE chill_main_postal_code DROP source'); + $this->addSql('ALTER TABLE chill_main_postal_code DROP center'); + } +} From 3770318e6a43769110a7f49af2164cb8167649c7 Mon Sep 17 00:00:00 2001 From: nobohan Date: Wed, 6 Oct 2021 21:48:56 +0200 Subject: [PATCH 2/6] enlarge the loadpostalcode command to more fields --- .../Command/LoadPostalCodesCommand.php | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Command/LoadPostalCodesCommand.php b/src/Bundle/ChillMainBundle/Command/LoadPostalCodesCommand.php index ba51db779..6b047a04b 100644 --- a/src/Bundle/ChillMainBundle/Command/LoadPostalCodesCommand.php +++ b/src/Bundle/ChillMainBundle/Command/LoadPostalCodesCommand.php @@ -19,6 +19,8 @@ namespace Chill\MainBundle\Command; +use Chill\MainBundle\Doctrine\Model\Point; +use Chill\MainBundle\Entity\Country; use Doctrine\ORM\EntityManager; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; @@ -69,6 +71,9 @@ class LoadPostalCodesCommand extends Command . "using the postal code and name. \n" . "The CSV file must have the following columns: " . "postal code, label, country code." + . "Optionally, the csv file can have the following " + . "columns after the country code: reference code, latitude, longitude, source. " + . "The latitude and longitude columns are supposed to be in WGS84 and expressed in decimal degrees. " . "The CSV file should not have any header row.") ->addArgument('csv_file', InputArgument::REQUIRED, "the path to " . "the csv file. See the help for specifications.") @@ -137,7 +142,7 @@ class LoadPostalCodesCommand extends Command $output->writeln(''.$num.' were added !'); } - private function getCSVResource(InputInterface $input) + public function getCSVResource(InputInterface $input) { $fs = new Filesystem(); $filename = $input->getArgument('csv_file'); @@ -163,7 +168,7 @@ class LoadPostalCodesCommand extends Command } $em = $this->entityManager; $country = $em - ->getRepository('ChillMainBundle:Country') + ->getRepository(Country::class) ->findOneBy(array('countryCode' => $row[2])); if ($country === NULL) { @@ -173,7 +178,7 @@ class LoadPostalCodesCommand extends Command // try to find an existing postal code $existingPC = $em - ->getRepository('ChillMainBundle:PostalCode') + ->getRepository(PostalCode::class) ->findBy(array('code' => $row[0], 'name' => $row[1])); if (count($existingPC) > 0) { @@ -187,6 +192,18 @@ class LoadPostalCodesCommand extends Command ->setCountry($country) ; + if (NULL != $row[3]){ + $postalCode->setRefId($row[3]); + } + + if (NULL != $row[4] & NULL != $row[5]){ + $postalCode->setCenter(Point::fromLonLat((float) $row[5], (float) $row[4])); + } + + if (NULL != $row[6]){ + $postalCode->setSource($row[6]); + } + $errors = $this->validator->validate($postalCode); if ($errors->count() == 0) { From fd73fac1a83fd353db21056a7de7a6769fa52cb9 Mon Sep 17 00:00:00 2001 From: nobohan Date: Thu, 7 Oct 2021 12:32:26 +0200 Subject: [PATCH 3/6] adapt migration --- .../ChillMainBundle/migrations/Version20211006151653.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Bundle/ChillMainBundle/migrations/Version20211006151653.php b/src/Bundle/ChillMainBundle/migrations/Version20211006151653.php index a6a66ce7e..c3853b568 100644 --- a/src/Bundle/ChillMainBundle/migrations/Version20211006151653.php +++ b/src/Bundle/ChillMainBundle/migrations/Version20211006151653.php @@ -19,14 +19,14 @@ final class Version20211006151653 extends AbstractMigration public function up(Schema $schema): void { - $this->addSql('ALTER TABLE chill_main_postal_code ADD refId VARCHAR(255) DEFAULT NULL'); + $this->addSql('ALTER TABLE chill_main_postal_code ADD refPostalCodeId VARCHAR(255) DEFAULT NULL'); $this->addSql('ALTER TABLE chill_main_postal_code ADD source VARCHAR(255) DEFAULT NULL'); $this->addSql('ALTER TABLE chill_main_postal_code ADD center geometry(POINT,4326) DEFAULT NULL'); } public function down(Schema $schema): void { - $this->addSql('ALTER TABLE chill_main_postal_code DROP refId'); + $this->addSql('ALTER TABLE chill_main_postal_code DROP refPostalCodeId'); $this->addSql('ALTER TABLE chill_main_postal_code DROP source'); $this->addSql('ALTER TABLE chill_main_postal_code DROP center'); } From 748fe28f8d558fb2c4c504ab009473b75e2c5b12 Mon Sep 17 00:00:00 2001 From: nobohan Date: Thu, 7 Oct 2021 13:42:26 +0200 Subject: [PATCH 4/6] postal code: rename fields to avoid confusion in load fixtures --- .../Command/LoadPostalCodesCommand.php | 4 ++-- .../ChillMainBundle/Entity/PostalCode.php | 20 +++++++++---------- .../migrations/Version20211006151653.php | 4 ++-- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Command/LoadPostalCodesCommand.php b/src/Bundle/ChillMainBundle/Command/LoadPostalCodesCommand.php index 6b047a04b..7655c2ed8 100644 --- a/src/Bundle/ChillMainBundle/Command/LoadPostalCodesCommand.php +++ b/src/Bundle/ChillMainBundle/Command/LoadPostalCodesCommand.php @@ -193,7 +193,7 @@ class LoadPostalCodesCommand extends Command ; if (NULL != $row[3]){ - $postalCode->setRefId($row[3]); + $postalCode->setRefPostalCodeId($row[3]); } if (NULL != $row[4] & NULL != $row[5]){ @@ -201,7 +201,7 @@ class LoadPostalCodesCommand extends Command } if (NULL != $row[6]){ - $postalCode->setSource($row[6]); + $postalCode->setPostalCodeSource($row[6]); } $errors = $this->validator->validate($postalCode); diff --git a/src/Bundle/ChillMainBundle/Entity/PostalCode.php b/src/Bundle/ChillMainBundle/Entity/PostalCode.php index cd7e8aa5f..8855c40f1 100644 --- a/src/Bundle/ChillMainBundle/Entity/PostalCode.php +++ b/src/Bundle/ChillMainBundle/Entity/PostalCode.php @@ -69,7 +69,7 @@ class PostalCode * @ORM\Column(type="string", length=255, nullable=true) * @groups({"write", "read"}) */ - private $refId; + private $refPostalCodeId; /** * @var string @@ -77,7 +77,7 @@ class PostalCode * @ORM\Column(type="string", length=255, nullable=true) * @groups({"write", "read"}) */ - private $source; + private $postalCodeSource; /** * @var Point @@ -195,26 +195,26 @@ class PostalCode return $this->country; } - public function getRefId(): ?string + public function getRefPostalCodeId(): ?string { - return $this->refId; + return $this->refPostalCodeId; } - public function setRefId(?string $refId): self + public function setRefPostalCodeId(?string $refPostalCodeId): self { - $this->refId = $refId; + $this->refPostalCodeId = $refPostalCodeId; return $this; } - public function getSource(): ?string + public function getPostalCodeSource(): ?string { - return $this->source; + return $this->postalCodeSource; } - public function setSource(?string $source): self + public function setPostalCodeSource(?string $postalCodeSource): self { - $this->source = $source; + $this->postalCodeSource = $postalCodeSource; return $this; } diff --git a/src/Bundle/ChillMainBundle/migrations/Version20211006151653.php b/src/Bundle/ChillMainBundle/migrations/Version20211006151653.php index c3853b568..9f4508ba8 100644 --- a/src/Bundle/ChillMainBundle/migrations/Version20211006151653.php +++ b/src/Bundle/ChillMainBundle/migrations/Version20211006151653.php @@ -20,14 +20,14 @@ final class Version20211006151653 extends AbstractMigration public function up(Schema $schema): void { $this->addSql('ALTER TABLE chill_main_postal_code ADD refPostalCodeId VARCHAR(255) DEFAULT NULL'); - $this->addSql('ALTER TABLE chill_main_postal_code ADD source VARCHAR(255) DEFAULT NULL'); + $this->addSql('ALTER TABLE chill_main_postal_code ADD postalCodeSource VARCHAR(255) DEFAULT NULL'); $this->addSql('ALTER TABLE chill_main_postal_code ADD center geometry(POINT,4326) DEFAULT NULL'); } public function down(Schema $schema): void { $this->addSql('ALTER TABLE chill_main_postal_code DROP refPostalCodeId'); - $this->addSql('ALTER TABLE chill_main_postal_code DROP source'); + $this->addSql('ALTER TABLE chill_main_postal_code DROP postalCodeSource'); $this->addSql('ALTER TABLE chill_main_postal_code DROP center'); } } From 665b0f8533e35bd83fb762301d72c3966aa837ae Mon Sep 17 00:00:00 2001 From: nobohan Date: Thu, 7 Oct 2021 13:48:05 +0200 Subject: [PATCH 5/6] postal code: adapt fixtures --- .../DataFixtures/ORM/LoadPostalCodes.php | 404 ++++++++++++++++-- 1 file changed, 364 insertions(+), 40 deletions(-) diff --git a/src/Bundle/ChillMainBundle/DataFixtures/ORM/LoadPostalCodes.php b/src/Bundle/ChillMainBundle/DataFixtures/ORM/LoadPostalCodes.php index 6e3d1781b..26ce7415e 100644 --- a/src/Bundle/ChillMainBundle/DataFixtures/ORM/LoadPostalCodes.php +++ b/src/Bundle/ChillMainBundle/DataFixtures/ORM/LoadPostalCodes.php @@ -22,6 +22,8 @@ namespace Chill\MainBundle\DataFixtures\ORM; +use Chill\MainBundle\Doctrine\Model\Point; +use Chill\MainBundle\Entity\Country; use Doctrine\Common\DataFixtures\AbstractFixture; use Doctrine\Common\DataFixtures\OrderedFixtureInterface; use Doctrine\Persistence\ObjectManager; @@ -40,21 +42,41 @@ class LoadPostalCodes extends AbstractFixture implements OrderedFixtureInterface return 50; } - public static $refs = array(); + public static $refs = []; public function load(ObjectManager $manager) { - $lines = str_getcsv(self::$codes, "\n"); - $belgium = $manager->getRepository('ChillMainBundle:Country') - ->findOneBy(array('countryCode' => 'BE')); + echo "loading postal codes... \n"; + $this->loadPostalCodeCSV($manager, self::$postalCodeBelgium, 'BE'); + $this->loadPostalCodeCSV($manager, self::$postalCodeFrance, 'FR'); + } + + private function loadPostalCodeCSV(ObjectManager $manager, string $csv, string $countryCode) { + + $lines = str_getcsv($csv, "\n"); + $country = $manager->getRepository(Country::class) + ->findOneBy(['countryCode' => $countryCode]); foreach($lines as $line) { $code = str_getcsv($line); $c = new PostalCode(); - $c->setCountry($belgium) + $c->setCountry($country) ->setCode($code[0]) - ->setName(\ucwords(\strtolower($code[2]))) + ->setName(\ucwords(\strtolower($code[1]))) ; + + if (NULL != $code[3]){ + $c->setRefPostalCodeId($code[3]); + } + + if (NULL != $code[4] & NULL != $code[5]){ + $c->setCenter(Point::fromLonLat((float) $code[5], (float) $code[4])); + } + + if (NULL != $code[6]){ + $c->setPostalCodeSource($code[6]); + } + $manager->persist($c); $ref = 'postal_code_'.$code[0]; @@ -66,40 +88,342 @@ class LoadPostalCodes extends AbstractFixture implements OrderedFixtureInterface $manager->flush(); } - - private static $codes = << Date: Fri, 8 Oct 2021 12:14:45 +0200 Subject: [PATCH 6/6] minor correction following MR review + Changelog --- CHANGELOG.md | 2 ++ .../ChillMainBundle/Command/LoadPostalCodesCommand.php | 2 +- src/Bundle/ChillMainBundle/Entity/PostalCode.php | 6 +++--- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9050e6e1c..5a490010a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ and this project adheres to ## Unreleased +* add 3 new fields to PostalCode and adapt postal code command and fixtures + * [Aside activity] Fixes for aside activity * categories with child diff --git a/src/Bundle/ChillMainBundle/Command/LoadPostalCodesCommand.php b/src/Bundle/ChillMainBundle/Command/LoadPostalCodesCommand.php index 7655c2ed8..21090a61f 100644 --- a/src/Bundle/ChillMainBundle/Command/LoadPostalCodesCommand.php +++ b/src/Bundle/ChillMainBundle/Command/LoadPostalCodesCommand.php @@ -142,7 +142,7 @@ class LoadPostalCodesCommand extends Command $output->writeln(''.$num.' were added !'); } - public function getCSVResource(InputInterface $input) + private function getCSVResource(InputInterface $input) { $fs = new Filesystem(); $filename = $input->getArgument('csv_file'); diff --git a/src/Bundle/ChillMainBundle/Entity/PostalCode.php b/src/Bundle/ChillMainBundle/Entity/PostalCode.php index 8855c40f1..91ce46e17 100644 --- a/src/Bundle/ChillMainBundle/Entity/PostalCode.php +++ b/src/Bundle/ChillMainBundle/Entity/PostalCode.php @@ -67,7 +67,7 @@ class PostalCode * @var string * * @ORM\Column(type="string", length=255, nullable=true) - * @groups({"write", "read"}) + * @groups({"read"}) */ private $refPostalCodeId; @@ -75,7 +75,7 @@ class PostalCode * @var string * * @ORM\Column(type="string", length=255, nullable=true) - * @groups({"write", "read"}) + * @groups({"read"}) */ private $postalCodeSource; @@ -83,7 +83,7 @@ class PostalCode * @var Point * * @ORM\Column(type="point", nullable=true) - * @groups({"write", "read"}) + * @groups({"read"}) */ private $center;