Zend Framework 2:如何为 ZfcUser 创建自己的用户映射器 [英] Zend Framework 2: How to create own User mapper for ZfcUser
问题描述
我正在处理我的第一个 Zend Framework 2 项目.我已经在我的项目中实现了 ZfcUser,并且由于我的数据库表中的列略有不同,我想从 ZfcUser 覆盖用户映射器.我已经通过互联网和这里进行了搜索,但没有找到可行的解决方案.我不得不承认我找到的解决方案是从 2012 年开始的(ZF2:自定义用户映射器对于 ZfcUser 模块),所以在此期间可能发生了一些变化.
I am working on my first Zend Framework 2 Project. I have implemented ZfcUser in my Project and since I have slightly different Columns in my Database Table, I want to overwrite the User Mapper from ZfcUser. I have searched through the Internet and on here, but did not get the solutions to work. I have to admit that the solution I found is from 2012 (ZF2: Custom user mapper for ZfcUser module), so maybe something has changed in the meantime.
到目前为止我做了什么.我已经创建了自己的用户实体.如果我理解正确,那么它需要扩展 ZfcUser,我已经这样做了:
So what have I done so far. I have created my own User entity. If I understood it right then it needs to extend ZfcUser which I have done like this:
module/Application/src/Entity/User.php
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
use ZfcUser\Entity\User as ZfcUser;
/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity
*/
class User extends ZfcUser
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="username", type="string", length=255, nullable=false)
*/
protected $username;
然后我复制了 zfcuser.global.php 并将其移动到文件夹 config/autoload/zfcuser.global.php
I then copied the zfcuser.global.php and moved it in the Folder config/autoload/zfcuser.global.php
在这个文件中我激活了'user_entity_class' => 'Application\Entity\User',我希望到目前为止这是正确的.
In this file I have activated 'user_entity_class' => 'Application\Entity\User', I hope this is correct so far.
现在我需要自己的 UserMapper.据我了解,这无法通过配置文件完成.我将文件夹Mapper"及其所有文件从 zfc-user/src/ZfcUser/Mapper 复制到我的应用程序模块中.我从所有文件中更改了命名空间.现在我很困惑我还需要复制什么,服务和/或工厂文件夹?我在哪里以及如何告诉我的应用程序使用我自己的 Mapper 而不是 ZfcUser?我只是不明白,希望有人能给我一些帮助.非常感谢您提前!
Now I need my own UserMapper. As far what I understood this can not be done through the config file. I copied the Folder "Mapper" with all its files from zfc-user/src/ZfcUser/Mapper into my Application Module. I changed the Namespace from all the files. Now I am confused what else I need to copy, the Service and/or the Factory Folder? And where and how do I tell my Application to use my own Mapper instead of ZfcUser? I just don't get it and hope someone can give me some assistance. Thank you very much in advance !
推荐答案
感谢 Ruben 和他的建议.我不得不稍微改变它才能让它工作,也许是因为我使用的是最新的 Zend Framework 2.
Thank you to Ruben and his suggestions. I had to change it slightly to get it to work maybe because I am using the latest Zend Framework 2.
我将在这里描述我的错误消息和解决方案,以防其他人遇到同样的问题.
I will describe here my Error messages and the solution, just in case someone else will face the same problem.
module.php
// This solution did not work for me
public function getServiceConfig()
{
return array(
'service_manager' => array(
'factories' => array(
// Override ZfcUser User Mapper factory
'zfcuser_user_mapper' => function ($sm) {
$options = $sm->get('zfcuser_module_options');
$mapper = new MyModule\Mapper\User($sm->get('Zend\Db\Adapter\Adapter'));
$entityClass = $options->getUserEntityClass();
$mapper->setHydrator(new ZfcUser\Mapper\UserHydrator());
$mapper->setTableName($options->getTableName());
return $mapper;
},
)
);
}
首先我必须删除'service_manager' => array(),否则代码将被完全忽略并使用普通的ZfcUser.在我删除后,我收到了 3 条错误消息.
First of all I had to remove 'service_manager' => array(), otherwise the code was totally ignored and the normal ZfcUser was used. After I removed that I received 3 Error Message.
1.可捕获的致命错误:传递给 ZfcUser\Mapper\UserHydrator::__construct() 的参数 1 必须是 Zend\Crypt\Password\PasswordInterface 的实例,没有给出,
为了修复它,我不得不改变它:
to fix it, I had to change this:
$mapper->setHydrator(new ZfcUser\Mapper\UserHydrator());
到这个代码:
$mapper->setHydrator($sm->get('zfcuser_user_hydrator'));
之后我收到了下一条错误消息:
after that I got the next Error message:
2.没有数据库适配器
再次需要更改一些代码:
Again some code needed changing:
$mapper = new MyModule\Mapper\User($sm->get('Zend\Db\Adapter\Adapter'));
进入这个
$mapper = new \ZfcUserExtension\Mapper\User();
$mapper->setDbAdapter($sm->get('zfcuser_zend_db_adapter'));
最后一条错误信息
3.没有实体原型集
当我添加以下行时,此消息消失了:
This message disappeared when I added the below line:
$mapper->setEntityPrototype(new $entityClass);
现在是最终的工作代码:
Here now the final working code:
public function getServiceConfig()
{
return array(
'factories' => array(
'zfcuser_user_mapper' => function ($sm) {
$options = $sm->get('zfcuser_module_options');
$mapper = new \ZfcUserExtension\Mapper\User();
// No db adapter present add below line
$mapper->setDbAdapter($sm->get('zfcuser_zend_db_adapter'));
$entityClass = $options->getUserEntityClass();
// No entity prototype set add below line
$mapper->setEntityPrototype(new $entityClass);
$mapper->setHydrator($sm->get('zfcuser_user_hydrator'));
$mapper->setTableName($options->getTableName());
return $mapper;
},
)
);
}
我最后将映射器和实体文件夹从 ZfcUser 复制到我的名为 ZfcUserExtension 的模型中.然后我在 UserMapper 中将数据库列名称从 user_id 更改为 id.我现在可以使用新的列名登录并注册.
I copied in the end the Mapper and Entity Folder from ZfcUser into my Model called ZfcUserExtension. I then changed in the UserMapper the Database column name from user_id to id. I can now Log in and Register with the new Column name.
不幸的是,我还不能使用 Change-Password,因为它仍在使用 user_id,我还没有找到它的设置位置以及如何覆盖它,但我会更新当我找到解决方案时发布.如果有人知道此问题的解决方案,请随时分享.
Unfortunately I am not able to use Change-Password yet, since this is still using user_id and I have not found out where this is set and how I can overwrite it, but I will update the post when I have found out a solution for it. If anyone knows the solution for this problem, then please feel free to share it.
非常感谢@Ruben 的帮助!!
A big Thank you @Ruben for his assistance !!
这篇关于Zend Framework 2:如何为 ZfcUser 创建自己的用户映射器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!