問題描述
我在我的 Zend Framework 2 應用程序中添加了模塊 ZfcUser.但我必須使用現有的數據庫表,它的列名與 ZfcUser 的默認表結構略有不同.
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.
在 ZfcUser wiki 頁面中,它說可以使用自定義如果我的模型不符合提供的接口,則映射器.而且由于我的數據庫表與默認值不同,因此我的用戶實體類也與標準 ZfcUserEntityUser.但是我可以告訴 ZfcUser 輕松地使用我自己的類通過覆蓋文件 config/autoload/zfcuser.global.php 中的設置:
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 ZfcUserEntityUser. 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' => 'MyAppEntityMyUser',
但到目前為止,我還沒有找到一種簡單的方法來告訴 ZfcUser 使用我的映射器類.
But I`ve not found an easy way to tell ZfcUser to use my mapper class so far.
我只發現映射器是由 ZfcUserModule::getServiceConfig() 創建的在其中,我可以看到映射器是從其工廠函數返回的:
I have only found that the mapper is created by ZfcUserModule::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 MapperUser();
$mapper->setDbAdapter($sm->get('zfcuser_zend_db_adapter'));
$entityClass = $options->getUserEntityClass();
$mapper->setEntityPrototype(new $entityClass);
$mapper->setHydrator(new MapperUserHydrator());
return $mapper;
},
// ...
有沒有辦法讓 ZfcUser 使用我的自定義用戶映射器類?
Is there a way to make ZfcUser to use my custom user mapper class?
推薦答案
我和你遇到了同樣的問題,但最后還是成功登錄了我的應用程序.我遵循了 Rob 的建議,并在我現有的用戶模塊中創建了自己的服務工廠.不幸的是,伯恩哈德也恰到好處.您必須深入研究 ZfcUser 源代碼才能使其工作.我現在從事的項目有一個 MSSQL 服務器,我必須說處理事情很困難.我最終只調整了 ZfcUser 源代碼中的一個函數來使登錄頁面正常工作.
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:
我將 Entity 和 Mapper 文件夾從 ZfcUser 目錄復制到我現有的 b2bUser(我的模塊)文件夾中.一切……甚至是 Mapper 中的 Exception 文件夾.可能沒有必要,但我沒心情弄清楚依賴關系.
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.
在 zfcuser.global.php 文件中,我的活動配置如下所示:
In the zfcuser.global.php file, my active configuration looks as follows:
'user_entity_class' => 'b2bUserEntityUser',
'enable_registration' => false,
'enable_username' => true,
'auth_identity_fields' => array( 'username' ),
'login_redirect_route' => 'home',
'enable_user_state' => false,
我將其余設置保留為默認值.我從身份驗證中刪除了電子郵件選項,因為他們不會使用電子郵件地址登錄系統.user_entity_class
是我復制的那個...
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)將以下內容復制到服務管理器配置中:
Module.php (b2bUser) Copied the following to the service manager config:
'zfcuser_user_mapper' => function ($sm) {
$mapper = new MapperUser();
$mapper->setDbAdapter($sm->get('ZendDbAdapterAdapter'));
$mapper->setEntityPrototype(new EntityUser());
$mapper->setHydrator(new MapperUserHydrator());
return $mapper;
},
設置完成后,我更改了 Entity 和 Mapper 中文件的命名空間等,以反映它們的新家.更改實體和接口以反映我自己的數據結構.我對 Mapper 文件做了同樣的處理,并確保 Hydrator 文件中的變量名稱與我的數據庫列名稱相同.
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.
我把 AbstractDbMapper 文件留在了原處.但這是我稍微調整了一下的文件.
I left the AbstractDbMapper file where it was. But this is the file I tweaked a bit.
這是我的樣子.SQLSRV 驅動程序充滿了 pooh,一直在抱怨一個對象或一個字符串......
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.
這篇關于ZF2:ZfcUser 模塊的自定義用戶映射器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!