From 994160f28a91b637b634caf277393903acd92b7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 3 Oct 2022 13:50:10 +0200 Subject: [PATCH] Feature: add a proper entity for geographical unit layer --- .../Entity/GeographicalUnit.php | 17 ++++-- .../Entity/GeographicalUnitLayer.php | 56 ++++++++++++++++++ .../migrations/Version20221003112151.php | 58 +++++++++++++++++++ 3 files changed, 125 insertions(+), 6 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Entity/GeographicalUnitLayer.php create mode 100644 src/Bundle/ChillMainBundle/migrations/Version20221003112151.php diff --git a/src/Bundle/ChillMainBundle/Entity/GeographicalUnit.php b/src/Bundle/ChillMainBundle/Entity/GeographicalUnit.php index 9e119e30d..fac5d9127 100644 --- a/src/Bundle/ChillMainBundle/Entity/GeographicalUnit.php +++ b/src/Bundle/ChillMainBundle/Entity/GeographicalUnit.php @@ -15,14 +15,14 @@ use Doctrine\ORM\Mapping as ORM; /** * @ORM\Table(name="chill_main_geographical_unit") - * @ORM\Entity + * @ORM\Entity(readOnly=true) */ class GeographicalUnit { /** * @ORM\Column(type="text", nullable=true) */ - private $geom; + private string $geom; /** * @ORM\Id @@ -32,14 +32,19 @@ class GeographicalUnit private ?int $id = null; /** - * @ORM\Column(type="string", length=255, nullable=true) + * @ORM\Column(type="text", nullable=false, options={"default": ""}) */ - private $layerName; + private string $unitName; /** - * @ORM\Column(type="string", length=255, nullable=true) + * @ORM\Column(type="text", nullable=false, options={"default": ""}) */ - private $unitName; + private string $unitRefId; + + /** + * @ORM\ManyToOne(targetEntity=GeographicalUnitLayer::class) + */ + private ?GeographicalUnitLayer $layer; public function getId(): ?int { diff --git a/src/Bundle/ChillMainBundle/Entity/GeographicalUnitLayer.php b/src/Bundle/ChillMainBundle/Entity/GeographicalUnitLayer.php new file mode 100644 index 000000000..76ac06e9c --- /dev/null +++ b/src/Bundle/ChillMainBundle/Entity/GeographicalUnitLayer.php @@ -0,0 +1,56 @@ +id; + } + + /** + * @return array + */ + public function getName(): array + { + return $this->name; + } + + /** + * @param array $name + * @return GeographicalUnitLayer + */ + public function setName(array $name): GeographicalUnitLayer + { + $this->name = $name; + return $this; + } +} \ No newline at end of file diff --git a/src/Bundle/ChillMainBundle/migrations/Version20221003112151.php b/src/Bundle/ChillMainBundle/migrations/Version20221003112151.php new file mode 100644 index 000000000..36b1834ea --- /dev/null +++ b/src/Bundle/ChillMainBundle/migrations/Version20221003112151.php @@ -0,0 +1,58 @@ +addSql('CREATE SEQUENCE chill_main_geographical_unit_layer_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); + $this->addSql('CREATE TABLE chill_main_geographical_unit_layer (id INT NOT NULL, name JSONB DEFAULT \'[]\'::jsonb NOT NULL, refid TEXT DEFAULT \'\' NOT NULL, PRIMARY KEY(id))'); + $this->addSql("COMMENT ON COLUMN chill_main_geographical_unit_layer.name IS '(DC2Type:json)';"); + + $this->addSql('INSERT INTO chill_main_geographical_unit_layer (id, name, refid) + SELECT DISTINCT nextval(\'chill_main_geographical_unit_layer_id_seq\'), jsonb_build_object(\'fr\', layername), layername FROM chill_main_geographical_unit'); + + $this->addSql('ALTER TABLE chill_main_geographical_unit ADD layer_id INT DEFAULT NULL'); + + $this->addSql('UPDATE chill_main_geographical_unit SET layer_id = layer.id FROM chill_main_geographical_unit_layer AS layer WHERE layer.refid = chill_main_geographical_unit.layername'); + + $this->addSql('ALTER TABLE chill_main_geographical_unit ADD unitRefId TEXT DEFAULT \'\' NOT NULL'); + $this->addSql('ALTER TABLE chill_main_geographical_unit DROP layername'); + $this->addSql("COMMENT ON COLUMN chill_main_geographical_unit.geom IS '(DC2Type:text)';"); + $this->addSql('ALTER TABLE chill_main_geographical_unit ALTER unitname TYPE TEXT'); + $this->addSql('ALTER TABLE chill_main_geographical_unit ALTER unitname SET DEFAULT \'\''); + $this->addSql('ALTER TABLE chill_main_geographical_unit ALTER unitname SET NOT NULL'); + $this->addSql('ALTER TABLE chill_main_geographical_unit ADD CONSTRAINT FK_360A2B2FEA6EFDCD FOREIGN KEY (layer_id) REFERENCES chill_main_geographical_unit_layer (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('CREATE INDEX IDX_360A2B2FEA6EFDCD ON chill_main_geographical_unit (layer_id)'); + + } + + public function down(Schema $schema): void + { + $this->throwIrreversibleMigrationException(); + + /* for memory + $this->addSql('ALTER TABLE chill_main_geographical_unit DROP CONSTRAINT FK_360A2B2FEA6EFDCD'); + $this->addSql('DROP SEQUENCE chill_main_geographical_unit_layer_id_seq CASCADE'); + $this->addSql('DROP TABLE chill_main_geographical_unit_layer'); + $this->addSql('ALTER TABLE chill_main_geographical_unit ADD layername VARCHAR(255) DEFAULT NULL'); + $this->addSql('ALTER TABLE chill_main_geographical_unit DROP layer_id'); + $this->addSql('ALTER TABLE chill_main_geographical_unit DROP unitRefId'); + $this->addSql('ALTER TABLE chill_main_geographical_unit ALTER geom TYPE VARCHAR(255)'); + $this->addSql('ALTER TABLE chill_main_geographical_unit ALTER unitName TYPE VARCHAR(255)'); + $this->addSql('ALTER TABLE chill_main_geographical_unit ALTER unitName DROP DEFAULT'); + $this->addSql('ALTER TABLE chill_main_geographical_unit ALTER unitName DROP NOT NULL'); + */ + } +}