简体   繁体   English

在 Sonata admin 中对 Fos 用户包角色字段使用下拉列表

[英]Use dropdown for Fos User bundle roles field in sonata admin

I'm using sonata admin to create an admin panel for FOS USER bundle users.我正在使用 Sonata admin 为 FOS USER 捆绑用户创建管理面板。 And I need to use a drop down for 'roles' filed.我需要使用下拉列表来提交“角色”。

This is my admin class,这是我的管理课,

<?php

namespace AdminBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;

class UserAdmin extends Admin {

    protected function configureFormFields(FormMapper $formMapper) {
        $formMapper->add('name', 'text');
        $formMapper->add('surname', 'text');
        $formMapper->add('username', 'text');
        $formMapper->add('email', 'text');
        $formMapper->add('telephone', 'text');
        $formMapper->add('password', 'text');
        $formMapper->add('roles', 'choice', array(
            'choices' => array(
                'Admin' => 'a:1:{i:0;s:10:"ROLE_ADMIN";}',
                'User' => 'a:0:{}',
            ),
            'choices_as_values' => true,
        ));
    }

But I'm getting this error,但我收到这个错误,

Notice: Array to string conversion 500 Internal Server Error - ContextErrorException注意:数组到字符串的转换 500 Internal Server Error - ContextErrorException

I think this is the part witch gives an error,我认为这是女巫出错的部分,

        $formMapper->add('roles', 'choice', array(
            'choices' => array(
                'Admin' => 'a:1:{i:0;s:10:"ROLE_ADMIN";}',
                'User' => 'a:0:{}',
            ),
            'choices_as_values' => true,
        ));

This a:1:{i:0;s:10:"ROLE_ADMIN";} is indeed an array (serialized).a:1:{i:0;s:10:"ROLE_ADMIN";}确实是一个数组(序列化)。 So you have to give a string representation of that, changing it in所以你必须给出一个字符串表示,将其更改为

$formMapper->add('roles', 'choice', array(
    'choices' => array(
        'Admin' => 'ROLE_ADMIN',
        'User' => '',
        ),
    'choices_as_values' => true,
));

or something like that.或类似的东西。

BTW as soon as you'll post that, you may need to manipulate your data manually as I'm not aware here how that field mapping is going to be tackle by the framework.顺便说一句,一旦您发布该内容,您可能需要手动操作您的数据,因为我不知道该框架将如何处理该字段映射。

What I can suggest, if no auto-magic happens, is to write your custom FormType to handle that situation.如果没有自动魔术发生,我可以建议的是编写您的自定义 FormType来处理这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM