I think there's a bug in EasyAdmin 3.5.9, but before creating an issue in GitHub, I would like to have people's opinion.
In my User CRUD, I wanted to be able to edit the user role, which wasn't displying by default. So I added it like so in UserCrudController
class:
public function configureFields(string $pageName): iterable
{
$roles = [
'User' => 'ROLE_USER',
'Agency' => 'ROLE_AGENCY',
'Admin' => 'ROLE_ADMIN'
];
return [
// ...
ChoiceField::new('roles')
->setChoices($roles)->allowMultipleChoices(),
// ...
];
}
Here is how it looks in User
entity:
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
$roles[] = 'ROLE_USER';
$roles[] = 'ROLE_AGENCY';
$roles[] = 'ROLE_ADMIN';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = array_values($roles);
return $this;
}
Actually, when I want to edit a user with EasyAdmin by selecting one of these choices, it save the right value in database. But when I come back on the same user to edit it, still all choices are displaying in the field, like I did nothing before. So it seems to be more a displaying issue than a data saving issue.
Here is a quick screen record to understand better: https://watch.screencastify.com/v/If9xScwQAFqcbbfGBmza
Hope you can help me.
CodePudding user response:
Your UserInterface::getRoles()
is manually adding all the roles back in with the set roles. The method should only need the following:
public function getRoles(): array
{
return array_unique($this->roles);
}