douturan1807 2013-10-07 07:07
浏览 57
已采纳

从codeigniter库调用模型显示错误

Hello I have created a library Auth.php in application/libraries to authenticate if the current user is logged in or not. So my controller is :

$session_data = $this->session->userdata('logged_in');
$this->load->library('Auth');
$auth = new Auth;
$user_id = $auth->authenticate($session_data);

And the library :

class Auth{
    function authenticate($vars){

        $CI =&get_instance();
        $CI->load->model('adminmodels/login_model');

        $username = $vars['username'];
        $password = $vars['password'];
        $user_id = $vars['user_id'];;
        $user_type = $vars['user_type'];

        $check_login = $this->login_model->login($username,$password); //Line 14

        if($check_login){
            $user_id = $user_id;
        }else{
            $user_id = 0;
        }

        return $user_id;
   }
}

But it is showing error like :

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Auth::$login_model

Filename: libraries/Auth.php

Line Number: 14

Whats wrong I am doing ??

  • 写回答

2条回答 默认 最新

  • dqq46733 2013-10-07 07:10
    关注

    While correct generally, in CI you should call the library like this, not with the new keyword:

    $this->load->library('Auth');
    $user_id = $this->auth->authenticate($session_data);
    

    Also, since you assigned the global CI object to a variable, you can't use $this to refer to it:

        $check_login = $this->login_model->login($username,$password); //Line 14
    

    should be:

        $check_login = $CI->login_model->login($username,$password); //Line 14
    

    (since you loaded the model there: $CI->load->model('adminmodels/login_model'); )

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog