ZF2:ZfcUser模块的自定义用户映射器 [英] ZF2: Custom user mapper for ZfcUser module

查看:84
本文介绍了ZF2: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页面中,它说可以使用自定义如果我的模型不符合所提供的接口,则为mapper. 而且由于我的数据库表与默认表不同,因此我的用户实体类也与 标准ZfcUser \ Entity \ User.但是我可以告诉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 ZfcUser\Entity\User. 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' => 'MyApp\Entity\MyUser',

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

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

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

I have only found that the mapper is created by ZfcUser\Module::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 Mapper\User();
                $mapper->setDbAdapter($sm->get('zfcuser_zend_db_adapter'));
                $entityClass = $options->getUserEntityClass();
                $mapper->setEntityPrototype(new $entityClass);
                $mapper->setHydrator(new Mapper\UserHydrator());
                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' => 'b2bUser\Entity\User',
'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 Mapper\User();
    $mapper->setDbAdapter($sm->get('Zend\Db\Adapter\Adapter'));
    $mapper->setEntityPrototype(new Entity\User());
    $mapper->setHydrator(new Mapper\UserHydrator());
    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驱动程序充满了声音,一直抱怨一个对象或字符串...

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.

这篇关于ZF2:ZfcUser模块的自定义用户映射器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆