编辑奏鸣曲用户数据

Posted

技术标签:

【中文标题】编辑奏鸣曲用户数据【英文标题】:Edit Sonata User data 【发布时间】:2019-10-13 00:29:39 【问题描述】:

我正在尝试在我的捆绑包中覆盖 Sonata 用户管理类。目前,我只是覆盖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 这些行:

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

【问题讨论】:

能否请教一下第二个 sn-p 的上下文?是configureFormFields()中的代码吗? 是的,是 ConfigureFormFields() 中的代码。 【参考方案1】:

如果您想从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。

【讨论】:

现在我编辑了 configureFormFields() 方法。它工作正常,但是当我对我的字段进行更改并尝试保存更改时,我遇到了这个错误:Call to a member function updateCanonicalFields() on null 我们可以在父母上找到它:public function preUpdate($user): void $this-&gt;getUserManager()-&gt;updateCanonicalFields($user); $this-&gt;getUserManager()-&gt;updatePassword($user); 好的,你能给我们看看你的奏鸣曲配置文件吗? 我编辑了我的第一篇文章并添加了奏鸣曲配置文件内容。 好的,谢谢,看来您需要额外的配置步骤才能使其正常工作,看看这个post。希望对您有所帮助。

以上是关于编辑奏鸣曲用户数据的主要内容,如果未能解决你的问题,请参考以下文章

以奏鸣曲管理员的编辑形式为用户设置角色

登录奏鸣曲系统用户

用户组的奏鸣曲验证不起作用

如何在用户/编辑上为管理员提供 SF/Sonata RepeatedType (pw)

如何在编辑时设置奏鸣曲形式 data_class

Sonata Admin - 只允许显示登录用户创建的内容