Relationship Entity User et Entity Role - symfony 6

30 Views Asked by At

I would like to know if it was possible to create a relationship between the User entity - $roles field (UserInterface implementations) and a Roles table - $id + $name fields which would store, among other things, all the roles

I would like to use a choiceType::class afterwards to get checkboxes to check :

  • expanded : false
  • multiple : true
#[ORM\Entity(repositoryClass: UserRepository::class)]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{

...

    #[ORM\Column(type: 'json')]
    private array $roles = [];

    /**
     * @see UserInterface
     */
    public function getRoles(): array
    {
        $roles = $this->roles;
        // guarantee every user at least has ROLE_USER
        $roles[] = 'ROLE_USER';

        return array_unique($roles);
    }
...

class Role
{
private $id
private $name
private $desc
...

If this relationship is possible, what is the nature of this relationship?

  • ManyToOne
  • ManyToMany

The $role field is of type Json. But I believe I know that a relationship between 2 entities is of Collection type. How to do ?

If you had a concrete example to help me.

Thanks for your help

0

There are 0 best solutions below