ZF2: Custom user mapper for ZfcUser module
Asked Answered
T

3

6

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?

Talbott answered 1/11, 2012 at 23:0 Comment(0)
R
4

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.

Ridenhour answered 15/12, 2012 at 2:58 Comment(1)
Big thanks! I was looking for the very same info, and with help of your post I managed to reload zfcUser's Entity and Mapper classes to be used with my DB and extended functionality of Entity!Hopi
P
4

Ceate your own service factory for zfcuser_user_mapper and it will get used.

Paleolith answered 24/11, 2012 at 14:21 Comment(2)
Yes that would be exactly what I am looking for. The question is how to do it.Talbott
add an entry for the zfcuser_user_mapper into your Module.php. ensure that your module is loaded after ZfcUser, it will overwrite the default. to see now zfcuser_user_mapper is configured, see the Module.php of ZfcUser. I am surprised that the issue of custom table has been out there for a while without a change in the official codebase. you may want to have a look at my fork at github.com/thoaionline/ZfcUserOvida
R
4

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.

Ridenhour answered 15/12, 2012 at 2:58 Comment(1)
Big thanks! I was looking for the very same info, and with help of your post I managed to reload zfcUser's Entity and Mapper classes to be used with my DB and extended functionality of Entity!Hopi
F
0

Currently there seems to be no easy way to change the table structure without modifying the ZfcUser source. There's a pull request on Github that should solve this problem:

https://github.com/ZF-Commons/ZfcUser/pull/174

Footboy answered 8/11, 2012 at 16:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.