尝试使用RoleUserBridge
,以便在注册时使用zfcuser
,user_role_linker
数据库表将自动更新。
我做了以下事情:
1)成功复制了vendor中的目录。
2) 复制linker.config.php 到config/autoconfig 目录
但是,使用 zfcuser 注册新用户不会将表条目添加到 user_role_linker 数据库。
我检查了工厂user_role_mapper
内部的功能RoleUserBridge/Module.php
'factories' => array(
'user_role_mapper' => function ($sm) {
$options = $sm->get('zfcuser_module_options');
$mapper = new Mapper\RoleMapper();
$mapper->setDbAdapter($sm->get('zfcuser_zend_db_adapter'));
$entityClass = $options->getUserEntityClass();
$mapper->setEntityPrototype(new $entityClass);
$mapper->setHydrator(new \ZfcUser\Mapper\UserHydrator());
return $mapper;
},
)
这被称为(我进行了die()
测试)。
RoleUserBridge\Mapper\RoleMapper.php
接下来,我在的插入函数中放了一个骰子。但是,这永远不会被调用。
所以,发生的事情是工厂正在创建一个实例,但插入函数永远不会被执行。
public function insert($entity, $tableName = null, HydratorInterface $hydrator = null)
{
$options = $this->getOptions();
$tableName = $options['user_role_linker'];
$result = parent::insert($entity, $tableName, $hydrator);
var_dump($entity);
var_dump($hydrator);
var_dump($result);
die();
return $result;
}
也许我没有正确链接一些东西......
我的加载顺序是:
'modules' => array(
// loads all 3rd party modules first
'ZfcBase',
'ZfcUser',
'ZfcAdmin',
'BjyAuthorize',
'RoleUserBridge',
...
请指教!
根据 Rajeeb 的反馈于 11 月 8 日编辑
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$serviceManager = $e->getApplication()->getServiceManager();
//zfcuser_user_service --- provided by ZfcUser module
$userService = $serviceManager->get('zfcuser_user_service');
//handling "register.post" event
$userService->getEventManager()->attach('register.post',
function(\Zend\EventManager\Event $e) use ($serviceManager) {
//get user entity from event params
$user = $e->getParam('user');
//this is my own "userrole" service, you can target to "userrolelinker" service
$userRoleService = $serviceManager->get('UserRole\Service\UserRoleService');
//below line is actually calling the insert functionallity via service
//if you would like can also be done by "mapper" class also without
//the involvement of "service" class
die('boo');
$userRoleService->insertUserRole(array(
'userId' => $user->getId(),
'roleId' => 3 //my target role id for the new users
));
}
);
}
干杯,
贾斯汀