Answer the question
In order to leave comments, you need to log in
How to properly set up a filter by roles in SonataAdminBundle?
I sifted through the SonataAdminBundle documentation, but did not find anything suitable.
If you specify in configureDatagridFilters like this:
the filter always works well, except for one case - when choosing advanced filters and is equal to
, everything is because of the tricky form of writing roles to the database: a:1:{i:0;s:9;" ROLE_USER";}
I decided to use a callback to search$datagridMapper->add('roles');
$datagridMapper
->add(
'roles',
CallbackFilter::class,
[
'operator_type' => TextType::class,
'callback' => [$this, 'filterByRoles'],
]
);
Answer the question
In order to leave comments, you need to log in
$datagridMapper->add(
'roles',
CallbackFilter::class,
[
'operator_type' => ChoiceType::class,
'callback' => [$this, 'filterByRoles'],
'operator_options' => [
'choices'=> [
"contains" => 1,
"not contains" => 2,
"is equals to" => 3,
],
],
],
ChoiceType::class,
[
'choices' => [
'USER' => 'ROLE_USER',
'ADMIN' => 'ROLE_ADMIN',
'SUPPORTER' => 'ROLE_SUPPORTER',
'COPYWRITER' => 'ROLE_COPYWRITER',
'SUPER_ADMIN' => 'ROLE_SUPER_ADMIN',
],
'multiple' => true,
]
);
If anything, the roles do not have to be stored as the symphony says in the documentation. It can also be simple strings. What matters is how you return the roles for the symphony. In order not to suffer much, you can do this:
->add('role', ChoiceType::class, [
'choices' => [
'Пользователь' => Role::ROLE_USER,
'Автор' => Role::ROLE_AUTHOR,
'Редактор' => Role::ROLE_EDITOR,
'Главред' => Role::ROLE_CHIEF_EDITOR,
'Администратор' => Role::ROLE_ADMIN,
],
'required' => false,
'placeholder' => 'Роль',
])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question