codeigniter离子auth获取用户ID [英] codeigniter ion auth getting user id
问题描述
我需要获取登录的用户的用户ID,以便我可以为该用户运行查询。 Im使用离子auth,应该使用会话还是从db调用它,我应该使用帮助?无论如何,heres我试图做什么:
I need to get the user id of the person logged in, so that i can run a query for that user. Im using ion auth, should i use a session or call it from the db, should i use a helper or not? anyway, heres what im trying to do:
示例:用户登录的工作订单有多少?
查询将是:select * from work_orders WHERE status =1AND userid =%THE LOGGED IN USER
example: how many work orders does logged in user have? query would be: select * from work_orders WHERE status = "1" AND userid = "%THE LOGGED IN USER"
这里是我的控制器,具有获取用户ID代码:
here is my controller, which doesnt have the "get user id" code:
public function index()
{
$this->load->view('templates/header');
$data['total_open_wo'] = $this->Home_model->total_open_wo();
$data['result'] = $this->Home_model->index();
$this->load->view('home', $data);
$this->load->view('templates/footer');
}
这里是我的模型:
public function total_open_wo() {
$this->db->where('status', "1");
$this->db->where('tid', "NEED_THE_USER_ID");
$num = $this->db->count_all_results('work_orders');
return ($num);
}
推荐答案
您可以使用离子授权中的 get_user_id()
函数来获取用户ID。在模型中的相关注释中,您需要一个 $ this-> db-> from()
调用来设置表以获取行数。
Provided the user is logged in you can get the user ID by using the get_user_id()
function in ion auth. On a related note in your model you need a $this->db->from()
call to set the table to fetch the number of rows from.
public function total_open_wo() {
$userId = $this->ion_auth->get_user_id();
$this->db->where('status', '1');
$this->db->where('tid', $userId);
$this->db->from('work_orders');
$num = $this->db->count_all_results();
return $num;
}
使用函数 $ this-> ion_auth- > logged_in()
以确保用户在调用模型函数之前已登录。
Use the function $this->ion_auth->logged_in()
to make sure that the user is logged in before calling the model function.
这篇关于codeigniter离子auth获取用户ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!