I have an User and a Group Entity which both hold an array of roles.
Now I would like to keep the option open to modificate the roles, add them and so on.
Should I use constants in the classes for this or should I relate an OneToOne-relation to a table which keeps all the roles?
Best Regards, pus.dev
User <=> Role Group <=> Role
public function getRoles()
{
$roles = $this->roles;
foreach ($this->getGroups() as $group) {
$roles = array_merge($roles, $group->getRoles());
}
// we need to make sure to have at least one role
$roles[] = static::ROLE_DEFAULT;
return array_unique($roles);
}
How about creating a Roles table with ManyToOne relation to each user. One row of the Roles table would contain a role(string or constant int) and a user.
Alternatively you can create a Roles table and have a ManyToMany relation with the User table. Using this you could define roles dynamically, so you don't have to hardcode the possible roles.
In the OneToMany case you could retrieve the roles by writing such a function:
OR
In the ManyToMany case you could retrieve the roles by writing such a function:
And dont forget that your User model must implement symfony's built-in User interface.
For group roles you can do this: