简体   繁体   English

无法将stdClass类型的对象用作数组codeigniter

[英]Cannot use object of type stdClass as array codeigniter

I'm building a project in CodeIgniter and trying to create a login functionality. 我正在CodeIgniter构建一个项目,并尝试创建login功能。

I just created a session on logging and passed the logged in user's id to the dashboard, but it showing me an error. 我只是在登录时创建了一个会话,并将登录的用户ID传递给了仪表板,但它向我显示了一个错误。 Here is my code. 这是我的代码。

My controller - login controller --------- 我的控制器-登录控制器---------

class Login extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('Auth_model','',true);
    }

    public function index(){
        $this->load->view('login/index');
    }

    public function verifyUser(){
        $userArray = array(
            "email"=>$this->input->post("email"),
            "password"=>$this->input->post("password"),
        );   
        $loggedUserData = $this->Auth_model->checkUser($userArray);  
        var_dump($loggedUserData);
        if(count($loggedUserData)>0){
            // this is where we creating session
           $this->session->set_userdata('loggedUserData',$loggedUserData);
           //redirect('dashboard/index');       
        }else{
           redirect('login');        
        }
    }
    public function logout(){
        $this->session->sess_destroy();
        redirect('login');
    }
}

My Model - Auth_model --------- 我的模型-Auth_model ---------

class Auth_model extends CI_model{  
    public function __construct() {
        parent::__construct();
    }
    public function checkUser($userArray){
        $loggedUserData = $this->db->select('id')
             ->where('email',$userArray["email"])
             ->where('password',$userArray["password"])
             ->get('users');
        return $loggedUserData->row();
        // here we are going to check the user with db
    }
}

My view - dashboard------------ 我的看法-仪表板------------

class Dashboard extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $currentUserData = $this->session->userdata();
        if(!isset($currentUserData["loggedUserData"])){
            redirect('/login/');
        }
    }
    public function index(){
        //$this->load->view('dashboard/index');
        echo $this->session->userdata['loggedUserData']['id'];
    }
}

Please help me out. 请帮帮我。

I believe the error is happening because of this call in the model. 我相信由于模型中的此调用而发生了错误。

return $loggedUserData->row();

The method row() returns and object, but you want an array. 方法row()返回和对象,但是您需要一个数组。 Change the above to 将以上更改为

return $loggedUserData->row_array();

and the error will go away. 错误就会消失。 Not to say everything will work. 并不是说一切都会正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM