From 9be8a533ff3b7390865eb32dea016ae3a6bdd3c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 25 Apr 2025 11:44:36 +0200 Subject: [PATCH] Add relation between UserGroup and UserJob Introduce a ManyToOne relationship between UserGroup and UserJob entities to allow synchronization of group members with corresponding UserJobs. This includes a schema migration to add the `userJob_id` column, associated constraints, and an index, as well as updates to the UserGroup entity with new methods for managing the relationship. --- .../ChillMainBundle/Entity/UserGroup.php | 35 +++++++++++++ .../migrations/Version20250425093948.php | 49 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 src/Bundle/ChillMainBundle/migrations/Version20250425093948.php diff --git a/src/Bundle/ChillMainBundle/Entity/UserGroup.php b/src/Bundle/ChillMainBundle/Entity/UserGroup.php index d720d4ffc..26a26431b 100644 --- a/src/Bundle/ChillMainBundle/Entity/UserGroup.php +++ b/src/Bundle/ChillMainBundle/Entity/UserGroup.php @@ -21,6 +21,19 @@ use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Serializer\Annotation\DiscriminatorMap; use Symfony\Component\Validator\Constraints as Assert; +/** + * Represents a user group entity in the system. + * + * This class is used for managing user groups, including their relationships + * with users, administrative users, and additional metadata such as colors and labels. + * + * Groups may be configured to have mutual exclusion properties based on an + * exclusion key. This ensures that groups sharing the same key cannot coexist + * in certain relationship contexts. + * + * Groups may be related to a UserJob. In that case, a cronjob task ensure that the members of the groups are + * automatically synced with this group. Such groups are also automatically created by the cronjob. + */ #[ORM\Entity] #[ORM\Table(name: 'chill_main_user_group')] // this discriminator key is required for automated denormalization @@ -71,6 +84,13 @@ class UserGroup #[Assert\Email] private string $email = ''; + /** + * UserJob to which the group is related. + */ + #[ORM\ManyToOne(targetEntity: UserJob::class)] + #[ORM\JoinColumn(nullable: true)] + private ?UserJob $userJob; + public function __construct() { $this->adminUsers = new ArrayCollection(); @@ -209,6 +229,21 @@ class UserGroup return '' !== $this->email; } + public function hasUserJob(): bool + { + return null !== $this->userJob; + } + + public function getUserJob(): ?UserJob + { + return $this->userJob; + } + + public function setUserJob(?UserJob $userJob): void + { + $this->userJob = $userJob; + } + /** * Checks if the current object is an instance of the UserGroup class. * diff --git a/src/Bundle/ChillMainBundle/migrations/Version20250425093948.php b/src/Bundle/ChillMainBundle/migrations/Version20250425093948.php new file mode 100644 index 000000000..ebd0f89b3 --- /dev/null +++ b/src/Bundle/ChillMainBundle/migrations/Version20250425093948.php @@ -0,0 +1,49 @@ +addSql(<<<'SQL' + ALTER TABLE chill_main_user_group ADD userJob_id INT DEFAULT NULL + SQL); + $this->addSql(<<<'SQL' + ALTER TABLE chill_main_user_group ADD CONSTRAINT FK_6576E74D64B65C5B FOREIGN KEY (userJob_id) REFERENCES chill_main_user_job (id) NOT DEFERRABLE INITIALLY IMMEDIATE + SQL); + $this->addSql(<<<'SQL' + CREATE INDEX IDX_6576E74D64B65C5B ON chill_main_user_group (userJob_id) + SQL); + } + + public function down(Schema $schema): void + { + $this->addSql(<<<'SQL' + ALTER TABLE chill_main_user_group DROP CONSTRAINT FK_6576E74D64B65C5B + SQL); + $this->addSql(<<<'SQL' + DROP INDEX IDX_6576E74D64B65C5B + SQL); + $this->addSql(<<<'SQL' + ALTER TABLE chill_main_user_group DROP userJob_id + SQL); + } +}