5

This is Database ERD that I use in application. I'm using Kohana 3.2. What I want to achieve is to generate menu for currently logged user. Each user can have many roles, so based on that user should get menu populated with modules (that are in relation with menu and user).

I have achieve this through several foreach loops. Is it possible to do this using ORM ?

*Table 'Modules' represents menu items.

Edit: this is my current code.

        $conf_modules = Kohana::$config->load('modules');
        $user_roles = $user->roles->find_all();

        $result     = array();
        $array  = array();

        foreach($user_roles as $user_role)
        {
            $menus   = $user_role->menus->find_all();
            $modules = $user_role->modules->find_all();
        }


        foreach($menus as $menu)
        {
            $m = $menu->modules->find_all();

            $result[]['name'] = $menu->name;

            foreach ($m as $a) 
            {
                foreach ($modules as $module) 
                {
                    if($a->name == $module->name)
                    {
                        foreach ($conf_modules as $key => $value) 
                        {
                            if($module->name == $key)
                            {
                                $array = array(
                                    'module_name' => $module->name,
                                    'text'        => $module->display_desc,
                                    'url'         => $value['url'],
                                );
                            }

                        }

                    }

                }
                array_push($result, $array);
            }

        }
4

1 回答 1

0

我认为这应该是一个很好的解决方案。

        $user = Auth::instance()->get_user();

        $user_roles = $user->roles->find_all();

        $conf_modules = Kohana::$config->load('modules');

        $role_modules = ORM::factory('module')
            ->join('roles_modules')
            ->on('roles_modules.module_id','=','module.id')
            ->where('role_id','IN',$user_roles->as_array(NULL,'id'))
            ->find_all();


        $role_menus = ORM::factory('menu')
            ->join('roles_menus')
            ->on('roles_menus.menu_id','=','menu.id')
            ->where('role_id','IN',$user_roles->as_array(NULL,'id'))
            ->find_all();

        $result = array();

        foreach ($role_menus as $role_menu) 
        {
            $menu_modules = $role_menu->modules->find_all();

            if ( ! isset($result[$role_menu->name]))
                $result[$role_menu->name] = array('name' => $role_menu->name);

            foreach ($menu_modules as $menu_module) 
            {
                foreach ($role_modules as $role_module) 
                {
                    if($menu_module->name == $role_module->name)
                    {
                        foreach ($conf_modules as $key => $value) 
                        {
                            if ($key == $role_module->name)
                            {
                                $result[$role_menu->name]['modules'][]['data'] = array('name' => $role_module->display_desc, 'url' => $value['url']);
                            }

                        }

                    }
                }   
            }   
        }

        return array_values($result);
于 2012-08-19T23:29:04.810 回答