ZF2:ZfcUser 模块的自定义用户映射器

2021-12-29 00:00:00 module config php zend-framework2 zfcuser

我在我的 Zend Framework 2 应用程序中添加了模块 ZfcUser.但我必须使用现有的数据库表,它的列名与 ZfcUser 的默认表结构略有不同.

I`ve added module ZfcUser on my Zend Framework 2 application. But I have to use existing database table, which has slightly different column names than the default table structure for ZfcUser.

在 ZfcUser wiki 页面中,它说可以使用自定义如果我的模型不符合提供的接口,则映射器.而且由于我的数据库表与默认值不同,因此我的用户实体类也与标准 ZfcUserEntityUser.但是我可以告诉 ZfcUser 轻松地使用我自己的类通过覆盖文件 config/autoload/zfcuser.global.php 中的设置:

In ZfcUser wiki page it says that it is possible to use custom mapper if my model doesn`t conform to the provided interface. And since my database table is different than default, my user entity class is also different than standard ZfcUserEntityUser. But I can tell ZfcUser to work with my own class easily by overriding setting in file config/autoload/zfcuser.global.php:

'user_entity_class' => 'MyAppEntityMyUser',

但到目前为止,我还没有找到一种简单的方法来告诉 ZfcUser 使用我的映射器类.

But I`ve not found an easy way to tell ZfcUser to use my mapper class so far.

我只发现映射器是由 ZfcUserModule::getServiceConfig() 创建的在其中,我可以看到映射器是从其工厂函数返回的:

I have only found that the mapper is created by ZfcUserModule::getServiceConfig() inside which, I can see the mapper is returned from its factory function:

// ...
public function getServiceConfig()
{
    return array(
    // ...
        'factories' => array(
            // ...
            'zfcuser_user_mapper' => function ($sm) {
                $options = $sm->get('zfcuser_module_options');
                $mapper = new MapperUser();
                $mapper->setDbAdapter($sm->get('zfcuser_zend_db_adapter'));
                $entityClass = $options->getUserEntityClass();
                $mapper->setEntityPrototype(new $entityClass);
                $mapper->setHydrator(new MapperUserHydrator());
                return $mapper;
            },
            // ...

有没有办法让 ZfcUser 使用我的自定义用户映射器类?

Is there a way to make ZfcUser to use my custom user mapper class?

推荐答案

我和你遇到了同样的问题,但最后还是成功登录了我的应用程序.我遵循了 Rob 的建议,并在我现有的用户模块中创建了自己的服务工厂.不幸的是,伯恩哈德也恰到好处.您必须深入研究 ZfcUser 源代码才能使其工作.我现在从事的项目有一个 MSSQL 服务器,我必须说处理事情很困难.我最终只调整了 ZfcUser 源代码中的一个函数来使登录页面正常工作.

I've had the same problem as you, but managed to log in to my application at last. I followed Rob's advice and created my own service factory within my existing user module. Unfortunately Bernhard is also spot on. You kinda have to dig into the ZfcUser source code to get it to work. The project I am working on now has a MSSQL server and I must say that it's been tough getting a handle on things. I've ended up tweaking only one function in the ZfcUser source to get the login page to work.

我只需要当前应用程序的登录功能,但即将到来的项目更多的是角色驱动.我一直在寻找一种不会太复杂以快速连接的东西,同时为未来提供更多选择和可能性.

I only need log in functionality for the current application, but the upcoming project is much more role driven. I was looking for something that wouldn't be too complicated to hook up quickly, and at the same time offer more options and possibilities for the future.

以下是我目前所做的以及我所学到的:

Here is what I did for now and what I've learned:

我将 Entity 和 Mapper 文件夹从 ZfcUser 目录复制到我现有的 b2bUser(我的模块)文件夹中.一切……甚至是 Mapper 中的 Exception 文件夹.可能没有必要,但我没心情弄清楚依赖关系.

I copied the Entity and Mapper folders from the ZfcUser directory over to my existing b2bUser (my module) folder. Everything...even the Exception folder inside Mapper. It might not be necessary, but I wasn't in the mood to figure out dependencies.

在 zfcuser.global.php 文件中,我的活动配置如下所示:

In the zfcuser.global.php file, my active configuration looks as follows:

'user_entity_class' => 'b2bUserEntityUser',
'enable_registration' => false,
'enable_username' => true,
'auth_identity_fields' => array( 'username' ),
'login_redirect_route' => 'home',
'enable_user_state' => false,

我将其余设置保留为默认值.我从身份验证中删除了电子邮件选项,因为他们不会使用电子邮件地址登录系统.user_entity_class 是我复制的那个...

I left the rest of the settings on default. I removed the email option from auth identities because they won't use email addresses to log into the system. The user_entity_class is the one I copied over...

Module.php (b2bUser)将以下内容复制到服务管理器配置中:

Module.php (b2bUser) Copied the following to the service manager config:

'zfcuser_user_mapper' => function ($sm) {
    $mapper = new MapperUser();
    $mapper->setDbAdapter($sm->get('ZendDbAdapterAdapter'));
    $mapper->setEntityPrototype(new EntityUser());
    $mapper->setHydrator(new MapperUserHydrator());
    return $mapper;
},

设置完成后,我更改了 Entity 和 Mapper 中文件的命名空间等,以反映它们的新家.更改实体和接口以反映我自己的数据结构.我对 Mapper 文件做了同样的处理,并确保 Hydrator 文件中的变量名称与我的数据库列名称相同.

After the setup was done, I changed the namespaces, etc of the files in Entity and Mapper to reflect their new home. Changed the Entity and the Interface to reflect my own data structure. I did the same with the Mapper files and made sure the variable names in the Hydrator file is the same as my database column names.

我把 AbstractDbMapper 文件留在了原处.但这是我稍微调整了一下的文件.

I left the AbstractDbMapper file where it was. But this is the file I tweaked a bit.

这是我的样子.SQLSRV 驱动程序充满了 pooh,一直在抱怨一个对象或一个字符串......

Here is what mine looks like. The SQLSRV driver was full of pooh, complaining the whole time about an object or a string...

protected function select(Select $select, $entityPrototype = null, HydratorInterface $hydrator = null)
{
    $this->initialize();
    $selectString = $this->getSlaveSql()->getSqlStringForSqlObject($select);
    $stmt = $this->getDbAdapter()->driver->createStatement($selectString);
    $stmt->prepare();
    $res = $stmt->execute($stmt);

    $resultSet = new HydratingResultSet($hydrator ?: $this->getHydrator(),
            $entityPrototype ?: $this->getEntityPrototype());
    $resultSet->initialize($res);
    return $resultSet;
}

就是这样.我希望它至少可以帮助某人在他们自己的系统上启动并运行它.我不会像这样离开我的,但让它发挥作用是一项使命.

And that is that. I hope it helps someone to get it up and running on their own system at least. I won't leave mine like this, but it was a bit of a mission to get it to work.

相关文章