codeigniter ion auth获取用户ID

bne*_*jax 7 codeigniter

我需要获取登录人员的用户ID,以便我可以为该用户运行查询.我使用离子身份验证,我应该使用会话或从数据库中调用它,我应该使用帮助器吗?无论如何,继承人试图做的事情:

示例:用户登录了多少个工单?查询将是:select*from work_orders WHERE status ="1"AND userid ="%THE LOGGED IN USER"

这是我的控制器,它没有"获取用户ID"代码:

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

}
Run Code Online (Sandbox Code Playgroud)

这是我的模特:

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);
    }
Run Code Online (Sandbox Code Playgroud)

Bad*_*olf 13

如果用户已登录,则可以使用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;
}
Run Code Online (Sandbox Code Playgroud)

$this->ion_auth->logged_in()在调用模型函数之前,使用该函数确保用户已登录.


Aru*_*unu 12

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

例如,获取用户ID

echo $this->ion_auth->user()->row()->id;
Run Code Online (Sandbox Code Playgroud)