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.

    评论

报告相同问题?