1

尝试使用RoleUserBridge,以便在注册时使用zfcuseruser_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
            ));
        }
    );
}

干杯,

贾斯汀

4

2 回答 2

1

您不需要 onBootStrap 部分。查看 RoleUserBridge 的代码,它覆盖了 zfcuser 模块。在我的 RoleUserBridge/Module.php 中,

  public function getServiceConfig()
{
    return array(
        'invokables' => array(
            'zfcuser_user_service' => 'RoleUserBridge\Service\User',
        ),
        'factories' => array(

            'user_role_mapper' => function ($sm) {
                $options = $sm->get('zfcuser_module_options');
                $config = $sm->get('config');
                $mapper = new Mapper\RoleMapper($config);
                $mapper->setDbAdapter($sm->get('zfcuser_zend_db_adapter'));
                $entityClass = $options->getUserEntityClass();
                $mapper->setEntityPrototype(new $entityClass);
                $mapper->setHydrator(new \ZfcUser\Mapper\UserHydrator());
                return $mapper;
            },
        )
    );
}

可调用部分覆盖 zfcuser_user_service 以使用 RoleUserBridge\Service\User ,只有此类使用 user_role_mapper。您可以通过确保您使用的 User 对象确实是 roleuserbridge 来开始调试,否则将不会使用 user_role_mapper。

希望这可以帮助

于 2014-03-18T22:41:16.960 回答
0

贾斯汀,这个桥接模块中的一切都很好,但是缺少关于如何实际执行插入操作的文档。我浏览了 Github 上的模块并找到了这个。您必须通过处理“zfcuser”抛出的“register.post”事件来实际调用 Service/Mapper 插入函数。查看下面的代码。

这是我的应用程序的“Module.php”类“bootstrap”事件处理程序

    //----keep what was there before--------
    $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 
        $userRoleService->insertUserRole(array(
            'userId' => $user->getId(),
            'roleId' => 3 //my target role id for the new users
        ));
    });

希望这会帮助你。你也可以用谷歌处理 zf2 事件。

于 2013-10-06T13:20:07.720 回答