7

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:

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"

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

}

here is my model:

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

2 回答 2

14

get_user_id()如果用户已登录,您可以使用ion auth 中的函数获取用户 ID 。在您的模型中的相关说明中,您需要$this->db->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()确保用户已登录。

于 2013-06-16T18:46:09.723 回答
13

您可以使用user()方法检索登录的用户详细信息。

例如获取用户ID

echo $this->ion_auth->user()->row()->id;
于 2013-06-16T18:59:50.947 回答