你好Guyzz我是Codeigniter的新手,我需要在系统中登录角色库方面获得帮助, 我在类别中有两个表第一个,第二个是user_reg,
在第二个存储有此类1,2,3的表category_id中,是否有可能获取此ID?
请给我建议和解决方案,我是CI的新手, 给我一些有关动态菜单和基于角色的登录访问的指导
答案 0 :(得分:3)
数据库表示例:
CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(100) NOT NULL,
`last_name` varchar(100) NOT NULL,
`username` varchar(100) NOT NULL,
`password` text NOT NULL,
`create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`user_id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
INSERT INTO `users` (`user_id`, `first_name`, `last_name`, `username`, `password`, `create_at`) VALUES
(1, 'ali', 'qorbani', 'aliqorbani', '63bed66f3d9dcd13440490d90738f816', '2018-07-28 08:34:01'),
(2, 'mohammad', 'ahmadi', 'mohammad', '316946a88ad51d75465c4c0b1e4a066a', '2018-07-28 08:34:01');
CREATE TABLE IF NOT EXISTS `user_groups` (
`group_id` int(11) NOT NULL AUTO_INCREMENT,
`group_name` varchar(100) NOT NULL,
`create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`group_id`),
KEY `group_id` (`group_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
INSERT INTO `user_groups` (`group_id`, `group_name`, `create_at`) VALUES
(1, 'superadmin', '2018-07-28 08:35:56'),
(2, 'admin', '2018-07-28 08:35:56'),
(3, 'moderator', '2018-07-28 08:36:12'),
(4, 'customer', '2018-07-28 08:36:12');
CREATE TABLE IF NOT EXISTS `user_roles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`group_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `id` (`id`),
KEY `user_id` (`user_id`),
KEY `group_id` (`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `user_roles`
ADD CONSTRAINT `user_roles_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `user_roles_ibfk_2` FOREIGN KEY (`group_id`) REFERENCES `user_groups` (`group_id`) ON DELETE CASCADE ON UPDATE CASCADE;
COMMIT;
型号的代码将是这样。
// User_model
public function add($data){
$this->db->insert('users',$data);
return true;
}
public function update($data,$id){
$this->db->where('user_id',$id);
$this->db->update('users',$data);
return true;
}
public function add_user_roles($user_id,$roles = array()){
$this->db->where('user_id',$user_id);
$this->db->delete('user_roles');
foreach ($roles as $role){
$this->db->insert('user_roles',['user_id'=>$user_id,'group_id'=>$role]);
}
return true;
}
,控制器 编辑方法的示例将如下所示:
//Users controller
public function edit($user_id){
if($this->form_validation->run() == FALSE) {
$data_view['user'] = $this->User_model->get_user($user_id);
$this->load->view('user_edit',$data_view);
}else{
$first_name = $this->input->post('first_name');
$last_name = $this->input->post('last_name');
$username = $this->input->post('username');
$password = md5($this->input->post('password'));
$user_roles = $this->input->post('roles');
$data_user_update = array(
'first_name' => $first_name,
'last_name' => $last_name,
'username' => $username,
'password' => $password
);
$this->User_model->add_user_roles$user_id,$user_roles);
$this->User_model->update($data_user_update,$user_id);
$this->session->set_flashdata('success','user updated correctly');
redirect('users/edit/'.$user_id);
}
}
答案 1 :(得分:1)
再也不会使用这种方式!
创建2个不同的表(用户,用户组)。在用户表group_id
中添加一列,然后使用join
函数获取主题
有关更多信息,请参见this link
如果您不确定,请在此处发布表格数据,我将为您提供帮助:-)
答案 2 :(得分:1)