ZF2: custom mapping module for the ZfcUser module

I added the ZfcUser module to my Zend Framework 2 application. But I have to use an existing database table that has slightly different column names than the default table structure for ZfcUser.

The ZfcUser wiki page says that you can use the user mapper if my model does not match the provided interface. And since my database table is different from the default, my user entity class is also different from the standard ZfcUser \ Entity \ User. But I can say that ZfcUser easily works with my own class by overriding the settings in the config / autoload / zfcuser.global.php file:

'user_entity_class' => 'MyApp\Entity\MyUser', 

But I did not find an easy way to tell ZfcUser to use my mapping class.

I just found that the collector was created by ZfcUser \ Module :: getServiceConfig () inside which I see that mapper is returning 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 force ZfcUser to use its own custom mapping class?

+6
source share
3 answers

I had the same problem as you, but I managed to log into my application. I followed Rob’s advice and created my own factory service in my existing user module. Unfortunately, Bernhard is also in place. You need to delve into the source code of ZfcUser to make it work. The project I'm working on now has an MSSQL server, and I have to say that it was difficult to get information about things. I finished setting up only one function in the ZfcUser source to make the login page work.

I only need the login function for the current application, but the upcoming project is much more role-oriented. I was looking for something that would not be too complicated to quickly connect and at the same time offer more opportunities and opportunities for the future.

Here is what I have done now and what I have learned:

I copied the Entity and Mapper folders from the ZfcUser directory to my existing b2bUser folder (my module). Everything ... even the Exclusions folder inside Mapper. Perhaps this is not necessary, but I was not in the mood to figure out the dependencies.

In the zfcuser.global.php file, my active configuration is 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 default settings. I removed the email option from authentication credentials because they will not use email addresses to log in. user_entity_class is the one I copied ...

Module.php (b2bUser) The following is copied to the service manager configuration:

 '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 completing the setup, I changed namespaces, etc. files in Entity and Mapper to reflect their new home. Entity and interface changed to reflect my own data structure. I did the same with the Mapper files and made sure that the variable names in the Hydrator file match the database column names.

I left the AbstractDbMapper file where it was. But this is a file that I changed a little.

This is what mine looks like. The SQLSRV driver was full of fluff, all the while complaining about an object or 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 it's all. Hope this helps someone up and work on their own system, at least. I will not leave mine like this, but it was a small mission to make it work.

+4
source

Set up your own factory service for zfcuser_user_mapper and it will be used.

+3
source

There is currently no easy way to change the structure of a table without changing the source of ZfcUser. Github has a migration request that should solve this problem:

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

0
source

All Articles