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条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器