[英]Zend Framework 2: How to use my own User table in ZfcUser
This is a follow up question to my previous question Zend Framework 2: How to create own User mapper for ZfcUser . 这是我之前的问题Zend Framework 2的后续问题:如何为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. 在我的用户数据库表中,与ZfcUser通常使用的user_id相比,我将id用作主键。 This was of course not possible. 这当然是不可能的。
Ok, so what have I done? 好的,那我做了什么? Here a summary. 这里总结一下。 I have created a new Module called ZfcUserExtension. 我创建了一个名为ZfcUserExtension的新模块。 I then copied the Mapper and Entity folder from ZfcUser. 然后,我从ZfcUser复制了Mapper and Entity文件夹。 I then overwrote the factory in the module.config.php. 然后,我在module.config.php中改写了工厂。 In the zfcuser.global.php I changed user_entity_class to reflect my Entity. 在zfcuser.global.php中,我更改了user_entity_class以反映我的实体。
I then changed user_id to id in the 3 Functions inside the User Mapper and UserHydrator.php 然后,我在User Mapper和UserHydrator.php的3个函数中将user_id更改为id
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: 之后,我尝试输入“ Change-Password”和“ Change-Email”,也就是当我收到错误消息时:
Unknown column 'user_id' in 'field list' . “字段列表”中的未知列“ user_id” 。
The way it looks, it actually goes into ZfcUser and uses this. 它的外观实际上是进入ZfcUser并使用它。 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? 我是否必须在模块ZfcUserExtension中为用户/更改密码和用户/更改电子邮件设置路由? 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? 但这并不意味着我也必须从ZfcUser复制控制器文件夹,或者我又该如何从ZfcUser使用控制器,而是从模块再次使用Mapper?
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 编辑:从bitWork获得帮助后,我有一个正在运行的解决方案
The below solution from bitWorking worked with only 1 change. 以下来自bitWorking的解决方案仅进行了1次更改。 I had to include the mapField calls and change the mapfield in the extract function. 我必须包括mapField调用,并在提取函数中更改mapfield。 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. 我不完全理解我为什么要这样做,因为我曾想过,如果我在两个函数中都将user_id更改为id,它应该可以工作,但是我发现由于没有可用的id,因此更新不起作用。 I also assumed that I then had to turn the mapping in the Hydrate function around, but that is not the case. 我还假设然后必须关闭Hydrate函数中的映射,但是事实并非如此。 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. 总而言之,如果我没有监督其他任何问题,我想说它现在可以使用id代替user_id作为主键。 Thanks a lot again to bitWorking for his great help ! 再次非常感谢bitWork对他的大力帮助!
ZfcUserExtension/Mapper/UserHydrator.php ZfcUserExtension /映射器/ 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);
}
}
Step by step: 一步步:
First override the factory in your Module.php or module.config.php. 首先,在您的Module.php或module.config.php中覆盖工厂。 You don't have to place it in the folder/namespace Factory\\Mapper
but for now I assume the same structure as ZfcUser. 您不必将其放在文件夹/命名空间Factory\\Mapper
但是现在我假定与ZfcUser相同的结构。
'factories' => array(
'zfcuser_user_hydrator' => 'ZfcUserExtension\Factory\Mapper\UserHydratorFactory',
),
Copy ZfcUser/Factory/Mapper/UserHydratorFactory.php
to ZfcUserExtension/Factory/Mapper/UserHydratorFactory.php
and change it: 将ZfcUser/Factory/Mapper/UserHydratorFactory.php
到ZfcUserExtension/Factory/Mapper/UserHydratorFactory.php
并进行更改:
namespace ZfcUserExtension\Factory\Mapper;
use Zend\Crypt\Password\Bcrypt;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use ZfcUserExtension\Mapper;
class UserHydratorFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$options = $serviceLocator->get('zfcuser_module_options');
$crypto = new Bcrypt;
$crypto->setCost($options->getPasswordCost());
return new Mapper\UserHydrator($crypto);
}
}
Now create the file ZfcUserExtension/Mapper/UserHydrator.php
(extend it and override the methods) 现在创建文件ZfcUserExtension/Mapper/UserHydrator.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);
return parent::extract($object);
}
public function hydrate(array $data, $object)
{
$this->guardUserObject($object);
return parent::hydrate($data, $object);
}
}
That should work if you want to change user_id
to id
. 如果您想将user_id
更改为id
那应该可以工作。 Else look at UserHydrator.php and change the mapField
calls. 否则,请查看UserHydrator.php并更改mapField
调用。 Note: Not tested.. 注意:未经测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.