From b78f0980f5b6f71b3d55183be021fd6d8c36cee3 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Thu, 26 Sep 2024 12:20:36 +0200 Subject: [PATCH] Create genderEnum, add genderTranslation property to Gender entity and new gender property to Person entity Also migrations were created to handle the changes in the database. --- src/Bundle/ChillMainBundle/Entity/Gender.php | 14 ++--- .../ChillMainBundle/Entity/GenderEnum.php | 11 ++++ .../migrations/Version20240926093955.php | 62 +++++++++++++++++++ .../ChillPersonBundle/Entity/Person.php | 25 ++++---- .../migrations/Version20240926100337.php | 41 ++++++++++++ 5 files changed, 135 insertions(+), 18 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Entity/GenderEnum.php create mode 100644 src/Bundle/ChillMainBundle/migrations/Version20240926093955.php create mode 100644 src/Bundle/ChillPersonBundle/migrations/Version20240926100337.php diff --git a/src/Bundle/ChillMainBundle/Entity/Gender.php b/src/Bundle/ChillMainBundle/Entity/Gender.php index 8bc94e473..a14f433c8 100644 --- a/src/Bundle/ChillMainBundle/Entity/Gender.php +++ b/src/Bundle/ChillMainBundle/Entity/Gender.php @@ -25,9 +25,9 @@ class Gender #[ORM\Column(type: \Doctrine\DBAL\Types\Types::BOOLEAN)] private bool $active = true; - #[Serializer\Groups(['read'])] - #[ORM\Column(type: \Doctrine\DBAL\Types\Types::BOOLEAN)] - private bool $isGrammatical = true; + #[Assert\NotNull(message: 'You must choose a gender translation')] + #[ORM\Column(type: \Doctrine\DBAL\Types\Types::STRING, enumType: GenderEnum::class)] + private GenderEnum $genderTranslation; #[Serializer\Groups(['read'])] #[ORM\Column(type: \Doctrine\DBAL\Types\Types::STRING, length: 255)] @@ -61,14 +61,14 @@ class Gender $this->active = $active; } - public function isGrammatical(): bool + public function getGenderTranslation(): GenderEnum { - return $this->isGrammatical; + return $this->genderTranslation; } - public function setIsGrammatical(bool $isGrammatical): void + public function setGenderTranslation(GenderEnum $genderTranslation): void { - $this->isGrammatical = $isGrammatical; + $this->$genderTranslation = $genderTranslation; } public function getIcon(): string diff --git a/src/Bundle/ChillMainBundle/Entity/GenderEnum.php b/src/Bundle/ChillMainBundle/Entity/GenderEnum.php new file mode 100644 index 000000000..d171ea343 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Entity/GenderEnum.php @@ -0,0 +1,11 @@ +addSql('CREATE SEQUENCE chill_main_gender_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); + $this->addSql('CREATE TABLE chill_main_gender (id INT NOT NULL, label JSON NOT NULL, active BOOLEAN NOT NULL, genderTranslation VARCHAR(255) NOT NULL, icon VARCHAR(255) NOT NULL, ordering DOUBLE PRECISION DEFAULT \'0.0\', PRIMARY KEY(id))'); + + // Insert the four gender records into the chill_main_gender table + $this->addSql(" + INSERT INTO chill_main_gender (id, label, active, genderTranslation, icon, ordering) + VALUES + (nextval('chill_main_gender_id_seq'), + '{\"fr\": \"homme\", \"nl\": \"man\"}', + true, + 'man', + '', + 1.0 + ), + (nextval('chill_main_gender_id_seq'), + '{\"fr\": \"femme\", \"nl\": \"vrouw\"}', + true, + 'woman', + '', + 1.1 + ), + (nextval('chill_main_gender_id_seq'), + '{\"fr\": \"neutre\", \"nl\": \"neutraal\"}', + true, + 'neutral', + '', + 1.1 + ), + (nextval('chill_main_gender_id_seq'), + '{\"fr\": \"inconnu\", \"nl\": \"ongekend\"}', + true, + 'unknown', + '', + 1.2 + ) + "); + } + + public function down(Schema $schema): void + { + $this->addSql('DROP SEQUENCE chill_main_gender_id_seq CASCADE'); + $this->addSql('DROP TABLE chill_main_gender'); + } +} diff --git a/src/Bundle/ChillPersonBundle/Entity/Person.php b/src/Bundle/ChillPersonBundle/Entity/Person.php index 0b4b54681..c8ba84797 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Person.php +++ b/src/Bundle/ChillPersonBundle/Entity/Person.php @@ -21,6 +21,7 @@ use Chill\MainBundle\Entity\Center; use Chill\MainBundle\Entity\Civility; use Chill\MainBundle\Entity\Country; use Chill\MainBundle\Entity\Embeddable\CommentEmbeddable; +use Chill\MainBundle\Entity\Gender; use Chill\MainBundle\Entity\HasCenterInterface; use Chill\MainBundle\Entity\Language; use Chill\MainBundle\Entity\User; @@ -59,19 +60,11 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface; #[HouseholdMembershipSequential(groups: ['household_memberships'])] class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateInterface, \Stringable { - final public const BOTH_GENDER = 'both'; - // have days in commun final public const ERROR_ADDIND_PERIOD_AFTER_AN_OPEN_PERIOD = 2; // where there exist final public const ERROR_PERIODS_ARE_COLLAPSING = 1; // when two different periods - final public const FEMALE_GENDER = 'woman'; - - final public const MALE_GENDER = 'man'; - - final public const NO_INFORMATION = 'unknown'; - /** * Accept receiving email. */ @@ -242,11 +235,11 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI private ?string $fullnameCanonical = ''; /** - * The person's gender. + * NEW column : The person's gender. */ #[Assert\NotNull(message: 'The gender must be set')] - #[ORM\Column(type: \Doctrine\DBAL\Types\Types::STRING, length: 9, nullable: true)] - private ?string $gender = null; + #[ORM\ManyToOne(targetEntity: Gender::class)] + private ?Gender $gender = null; /** * Comment on gender. @@ -1657,6 +1650,16 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI return $this; } + public function getGenderKind(): ?Gender + { + return $this->genderKind; + } + + public function setGenderKind(?Gender $genderKind): void + { + $this->genderKind = $genderKind; + } + private function getCurrentCenterHistory(): ?PersonCenterHistory { if (0 === $this->centerHistory->count()) { diff --git a/src/Bundle/ChillPersonBundle/migrations/Version20240926100337.php b/src/Bundle/ChillPersonBundle/migrations/Version20240926100337.php new file mode 100644 index 000000000..021533efd --- /dev/null +++ b/src/Bundle/ChillPersonBundle/migrations/Version20240926100337.php @@ -0,0 +1,41 @@ +addSql('ALTER TABLE chill_person_person ADD gender_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE chill_person_person ADD CONSTRAINT FK_BF210A14708A0E0 FOREIGN KEY (gender_id) REFERENCES chill_main_gender (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('CREATE INDEX IDX_BF210A14708A0E0 ON chill_person_person (gender_id)'); + + // transfer gender values to point to corresponding gender entity within new column + $this->addSql(" + UPDATE chill_person_person AS p + SET gender_id = g.id + FROM chill_main_gender AS g + WHERE g.genderTranslation = p.gender AND p.gender IN ('man', 'woman', 'unknown') + OR (g.genderTranslation = 'neutral' AND p.gender = 'both') + "); + + // delete old gender column + $this->addSql('ALTER TABLE chill_person_person DROP gender'); + } + + public function down(Schema $schema): void + { + $this->addSql('ALTER TABLE chill_person_person ADD gender VARCHAR(9) DEFAULT NULL'); + $this->addSql('ALTER TABLE chill_person_person DROP gender_id'); + } +}