dougan1884 2013-07-04 05:50
浏览 34

ZF2 RoleUserBridge:注册后未插入

Trying to use RoleUserBridge so that when registering using zfcuser, user_role_linker database table will get updated automatically.

I have done the following:

1) successfully copied the directory in vendor.
2) copy linker.config.php to config/autoconfig directory

But, registering a new user using zfcuser doesn't add a table entry to user_role_linker database.

I checked the user_role_mapper function inside RoleUserBridge/Module.php's factories

'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;
            },
        )

This gets called (I put a die() to test).

Next, I put a die in RoleUserBridge\Mapper\RoleMapper.php's insert function. But, this never gets called.

So, what's happening is that the factory is creating an instance, but the insert function never gets executed.

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;
}

Maybe I'm not linking something correctly...

My load order is:

'modules' => array(
    // loads all 3rd party modules first
    'ZfcBase',
    'ZfcUser',
    'ZfcAdmin',
    'BjyAuthorize',
    'RoleUserBridge',
    ...

Please advice!

edit on Nov 8th based on Rajeeb's feedback

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
            ));
        }
    );
}

Cheers,

Justin

  • 写回答

2条回答 默认 最新

  • doujiao9426 2013-10-06 13:20
    关注

    Well Justin, everything is fine in this bridge module but lacking the documentation about how to actually execute the insert action. I have gone through the module on Github and found this. You have to actually make a call to the Service/Mapper insert function by handling the "register.post" event thrown by "zfcuser". Check out the code below.

    This is my Application's "Module.php" class "bootstrap" event handler

        //----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
            ));
        });
    

    hope this will help you. Also you may google handling zf2 events.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。