简体   繁体   中英

Unable to load model inside Controller - Codeigniter

For some reason my model is not loading properly (i think), or at least I can not access its methods.

class Main extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('user');
    }
    public function index() {
        $this->load->helper(array('form'));
        $this->load->view('login');
    }
    public function login() {
        if(isset($_POST['username'])) {
            $username = $_POST['username'];
        }
        if(isset($_POST['password'])) {
            $password = $_POST['password'];
        }
        // tried loading module here as well with no success
        $data = array('username'=>$username, 'password'=>$password);        
        $this->model->validateUser($data);
    }
}

Model

class User extends CI_Model {

    public function __construct() {        
        parent::__construct();
    }

    public function validateUser($data)  {
        var_dump($data[0]." ".$data[1]);
    }
}

This is what it returns me Call to a member function validateUser() on a non-object I do not really see what and where is something wrong ? perhaps 2nd pair of eyes can help :) I am using CI 3+.

My advice for CI is to setup your Models with suffix (rename and the file)..

class User_model extends CI_Model 

after that in your controller..

$this->load->model('user_model');
$this->user_model->dosomething();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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