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.
This commit is contained in:
Julien Fastré 2025-04-25 11:44:36 +02:00
parent b414b27ba9
commit 9be8a533ff
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
2 changed files with 84 additions and 0 deletions

View File

@ -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.
*

View File

@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\Migrations\Main;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20250425093948 extends AbstractMigration
{
public function getDescription(): string
{
return 'Create a relation between usergroup and user job';
}
public function up(Schema $schema): void
{
$this->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);
}
}