1

Does anyone know how i can get rid of this error: " Fatal error: Call to undefined method CI_Model::User_model()"

This is my user.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
    class User extends CI_Controller{    
        function user_model(){
            parent::User_model();
            $this->load->model('user_model','',TRUE);
        }
    public function index(){
        $this->login();
    }   
    function login(){
    //xss_clean doesn't work...
        $this->form_validation->set_rules('username', 'Username',                                  
        'required|trim|max_length[50]');
        $this->form_validation->set_rules('password', 'Password',     
        'required|trim|max_length[150]');       
        if ($this->form_validation->run() == FALSE){
            $this->load->view('view_login');
        }
        else{   
        }
    }
}
?>

This is the user_model.php

<?php
class User_model extends CI_Model {
    function user_model(){
        parent::User_model();
    }
    function check_login($username, $password){
        $sha1_password = sha1($password);
        $query_str = "SELECT user_id FROM users WHERE username = ? and          
        password = ?";
        $result = $this->db->query($query_str, array($username,         
        $sha1_password));
        if ($result->num_rows() == 1){
            return $result->row(0)->user_id;
        }
        else{
            return false;
        }
    }
}
?>

This is my autoload:$autoload['model'] = array('User_model');

I am new to codeigniter and I was following a tutorial, in the video everything went fine, but I get errors ofcrs. Maybe it's an easy answer, but I couldn't get it.

Zikria Azimi
  • 49
  • 1
  • 7
  • 1
    Why do you call `parent::User_model();` in both classes? Are you sure this method exists? – tkausl Jun 22 '16 at 16:25

1 Answers1

2

The issues below are some of the most obvious ones. I strongly urge that you go back to the documentation and check everything more thoroughly.


Refer to the examples in the documentation for constructing your model.

<?php

class User_model extends CI_Model {
    public function __construct()
    {
        // Call the CI_Model constructor
        parent::__construct();
    }
    function check_login($username, $password)
    { ....

And then refer to the docs for your controller...

<?php

    class User extends CI_Controller {    

        public function index()
        { ....

For loading, pay closer attention to the upper/lower case in the spelling...

$autoload['model'] = array('user_model');

It's all supposed to spelled in lower-case when you're referring to it.

Sparky
  • 98,165
  • 25
  • 199
  • 285