doucang5542 2012-11-01 23:00
浏览 31
已采纳

ZF2: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.

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',

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

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;
            },
            // ...

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

  • 写回答

3条回答 默认 最新

  • doulong2782 2012-12-15 02:58
    关注

    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:

    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.

    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,
    

    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) 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;
    },
    

    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.

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

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证