编辑Sonata用户数据

时间:2019-05-27 12:17:16

标签: symfony symfony4 sonata-admin sonata sonata-user-bundle

我正在尝试覆盖我的捆绑软件中的Sonata User Admin类。目前,我只是覆盖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'])
            ;
        }
    }
}

我刷新了页面,我得到了没有问题的用户列表。但是,当我单击用户对其进行编辑时,出现以下错误:在以下行上调用成员函数getClass()为空

$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)%"

1 个答案:

答案 0 :(得分:1)

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

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