8uld0zr
8uld0zr

Reputation: 69

Role doesn't appear in database, but `getRoles` should always add ROLE_USER

I tried to store a user in the database.

When I create a new user, the role doesn't appear in database, even if my function inside my "candidate" entity says that any user should have at least the role USER_ROLE

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

       return array_unique($roles);
}

Here is what I'm talking about

Do you know what I am doing wrong?

Upvotes: 0

Views: 157

Answers (1)

yivi
yivi

Reputation: 47369

The getter method is not called when storing data in the DB.

First the object is retrieved from the DB and its properties populated. getRoles() read from those properties (specifically from $this->roles, which should reflect the DB content.

What getRoles() does is make sure that even if you didn't assign any role to a specific user (as the case for your second row), the application will see that the user has the USER_ROLE assigned.

Upvotes: 2

Related Questions