编辑奏鸣曲用户数据

Edit Sonata User data

我试图在我的包中覆盖 Sonata 用户管理 class。目前,我只是覆盖 configureListFields 方法: 命名空间 App\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;

use Sonata\UserBundle\Admin\Model\UserAdmin as BaseUserAdmin;

class UserAdmin extends BaseUserAdmin
{

    /**
     * @param ListMapper $listMapper
     */
    protected function configureListFields(ListMapper $listMapper):void
    {
        $listMapper
            ->addIdentifier('username')
            ->add('email')
            ->add('groups')
            ->add('enabled', null, ['editable' => true])
            ->add('accountType')
            ->add('createdAt')
        ;

        if ($this->isGranted('ROLE_ALLOWED_TO_SWITCH')) {
            $listMapper
                ->add('impersonating', 'string', ['template' => '@SonataUser/Admin/Field/impersonating.html.twig'])
            ;
        }
    }
}

我刷新了我的页面,并且毫无问题地获得了用户列表。但是当我点击一个用户来编辑它时,我有这个错误:Call to a member function getClass() on null on these lines:

$now = new \DateTime();

        $genderOptions = [
            'choices' => \call_user_func([$this->getUserManager()->getClass(), 'getGenderList']),
            'required' => true,
            'translation_domain' => $this->getTranslationDomain(),
        ];

        // NEXT_MAJOR: Remove this when dropping support for SF 2.8
        if (method_exists(FormTypeInterface::class, 'setDefaultOptions')) {
            $genderOptions['choices_as_values'] = true;
        }

Sonata_admin.yaml 文件:

sonata_admin:
    title: 'Staff Admin Panel'
    templates:
        dashboard: '@SonataAdmin/Core/dashboard.html.twig'
    security:
        handler: sonata.admin.security.handler.role
        role_admin: ROLE_ADMIN
        role_super_admin: ROLE_SUPER_ADMIN
        # information:
        #     GUEST: [VIEW, LIST]
        #     STAFF: [EDIT, LIST, CREATE]
        #     EDITOR: [OPERATOR, EXPORT]
        #     ADMIN: [MASTER]

        # admin_permissions: [CREATE, LIST, DELETE, UNDELETE, EXPORT, OPERATOR, MASTER]
        # object_permissions: [VIEW, EDIT, DELETE, UNDELETE, OPERATOR, MASTER, OWNER]

    dashboard:
        blocks:
            - { type: sonata.admin.block.admin_list, position: left }
        groups:
            content:
                label: Contenu
                icon: '<i class="fa fa-file-text-o"></i>'
                items:
                    - app.admin.specialty
                    - app.admin.cities
                    - app.admin.colleges
                    - app.admin.building

            sonata.admin.group.media:
                label: Médiathèque
                icon: '<i class="fa fa-camera-retro"></i>'
                items:
                    - sonata.media.admin.media

            settings:
                label: Paramètres
                icon: '<i class="fa fa-cog"></i>'
                items:
                    - sonata.classification.admin.category
                    - sonata.classification.admin.context
                    - sonata.classification.admin.tag
                    - sonata.classification.admin.collection
                    - app.admin.icon

            sonata.admin.group.administration:
                label: Utilisateur et Groupes
                label_catalogue: SonataAdminBundle
                icon: '<i class="fa fa-users"></i>'
                items:
                    - app.admin.user
                    - sonata.user.admin.group

sonata_block:
    blocks:
        sonata.admin.block.admin_list:
            contexts: [admin]

sonata_user:
    security_acl: true
    manager_type: orm
    class:
        user: App\Application\Sonata\UserBundle\Entity\User
        group: App\Application\Sonata\UserBundle\Entity\Group

fos_user.yaml 文件:

fos_user:
    db_driver: orm # valid values are 'orm', 'mongodb' and 'couchdb'
    user_class: App\Entity\User #App\Application\Sonata\UserBundle\Entity\User
    firewall_name: main

    registration:
        form:
            type: App\Application\Sonata\UserBundle\Form\RegistrationType

    group:
        group_class: App\Application\Sonata\UserBundle\Entity\Group
        group_manager: sonata.user.orm.group_manager

    service:
        user_manager: sonata.user.orm.user_manager
        mailer: fos_user.mailer.noop

    from_email:
        address: "%env(MAILER_SENDER_ADDRESS)%"
        sender_name: "%env(MAILER_SENDER_NAME)%"

如果您想从 configureFormFields() 访问您的方法 getGenderList(),您可以这样做:

protected function configureFormFields(FormMapper $formMapper)
{
        $now = new \DateTime();
        $genderOptions = [
            'choices' => $this->getSubject()->getGenderList(), // here
            'required' => true,
            'translation_domain' => $this->getTranslationDomain(),
        ];

        // NEXT_MAJOR: Remove this when dropping support for SF 2.8
        if (method_exists(FormTypeInterface::class, 'setDefaultOptions')) {
            $genderOptions['choices_as_values'] = true;
        }
}

参见Symfony docs about it