Q : what is the purpose of using groups if role management can only be managed from a user object
A : The answer can be extracted from Using Groups with FOSUserBundle :
Symfony2 , . , ( ).
Q: : ? , ?
A. , "" , (, ). , ROLE_ADMIN MANAGER, REVISER ( , ). . , , , , .
Q: User Group entity - ?
A. , "", User ā Group ā Role, :
:
use Doctrine\Common\Collections\ArrayCollection;
protected $groups;
public function __construct()
{
$this->groups = new ArrayCollection(); <-- add this line to the constructor
}
public function getGroups()
{
return $this->groups;
}
public function addGroup(Group $group)
{
if (!$this->groups->contains($group)) {
$this->groups->add($group);
}
}
public function removeGroup(Group $group)
{
if ($this->groups->contains($group)) {
$this->groups->removeElement($group);
}
}
public function getRoles()
{
$roles = [];
$groups = $this->getGroups();
foreach ($groups as $group) {
$roles = array_merge($roles, $group->getRoles()->toArray());
}
$roles = array_unique($roles);
$this->roles = $roles;
return $roles;
}
:
protected $roles;
public function __construct()
{
$this->roles = new ArrayCollection();
}
public function getRoles()
{
return $this->roles;
}
public function addRole(Role $role)
{
if (! $this->roles->contains($role)) {
$this->roles->add($role);
}
}
public function removeRole(Role $role)
{
if ($this->roles->contains($role)) {
$this->roles->removeElement($role);
}
}
. , .