Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZF2: Custom user mapper for ZfcUser module

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?

like image 586
Luke 10X Avatar asked Nov 01 '12 23:11

Luke 10X


2 Answers

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.

like image 67
corl Avatar answered Oct 06 '22 10:10

corl


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

like image 30
Rob Allen Avatar answered Oct 06 '22 09:10

Rob Allen