This is a follow up question to my previous question Zend Framework 2: How to create own User mapper for ZfcUser.
What was the problem? In my User Database Table, I use id as primary key in comparison to user_id which ZfcUser is normally using. This was of course not possible.
Ok, so what have I done? Here a summary. I have created a new Module called ZfcUserExtension. I then copied the Mapper and Entity folder from ZfcUser. I then overwrote the factory in the module.config.php. In the zfcuser.global.php I changed user_entity_class to reflect my Entity.
I then changed user_id to id in the 3 Functions inside the User Mapper and UserHydrator.php
I can now log in with now problem and register. After that I tried to "Change-Password" and "Change-Email" and that is when I get the Error Message occurred:
Unknown column 'user_id' in 'field list'.
The way it looks, it actually goes into ZfcUser and uses this. So my question now is, what else do I have to change or copy? Am I right that I have to set up a route in my Module ZfcUserExtension for User/change-password and User/change-email? But does it not mean I then have to copy the Controller Folder from ZfcUser as well or how can I use the controller from ZfcUser, but then use the Mapper again from my Module?
Does someone got an idea how I can solve it? I am really lost here. Thank you very much in advance.
EDIT: after receiving help from bitWorking I have a running solution
The below solution from bitWorking worked with only 1 change. I had to include the mapField calls and change the mapfield in the extract function. I don't fully understand why I had to do that, because I would have thought that if I change user_id to id in both functions it should work, but figured out that the update then doesn't work because there is no id available. I also assumed that I then had to turn the mapping in the Hydrate function around, but that is not the case. All in all, if I have not overseen any other issue, I would say it now works with id instead of user_id as a primary key. Thanks a lot again to bitWorking for his great help !
ZfcUserExtension/Mapper/UserHydrator.php
<?php
namespace ZfcUserExtension\Mapper;
use Zend\Crypt\Password\PasswordInterface as ZendCryptPassword;
use Zend\Stdlib\Hydrator\ClassMethods;
use ZfcUser\Mapper\UserHydrator as ZfcUserUserHydrator;
class UserHydrator extends ZfcUserUserHydrator
{
public function extract($object)
{
$this->guardUserObject($object);
$data = parent::extract($object);
// return $this->mapField('id', 'user_id', $data); //original from ZfcUser
return $this->mapField('user_id', 'id', $data); // now it works
}
public function hydrate(array $data, $object)
{
$this->guardUserObject($object);
$data = $this->mapField('user_id', 'id', $data);
return parent::hydrate($data, $object);
}
}