Peter Stuart
Peter Stuart

Reputation: 2444

CodeIgniter model not loading correctly

I am writting my first CodeIgniter script and I can't get the following model to load, if anyone could maybe help me?

Here is my controller file:

public function process(){
// Load the model
$this->load->model('users/login', 'users');

// Validate the user can login
$result = $this->users->validate();

// Now we verify the result
if(!$result){
    // If user did not validate, then show them login page again
    $this->index();
}else{
    // If user did validate,
    // Send them to members area
    redirect('home');
}      
}

And here is my model

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Login_Model extends CI_Model{
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    public function validate(){

        // grab user input
        $username = $this->security->xss_clean($this->input->post('username'));
        $password = $this->security->xss_clean($this->input->post('password'));

        // Prep the query
        $this->db->where('username', $username);
        $this->db->where('password', $password);

        // Run the query
        $query = $this->db->get('users');
        // Let's check if there are any results
        if($query->num_rows == 1)
        {
            // If there is a user, then create session data
            $row = $query->row();
            $data = array(
                    'userid' => $row->userid,
                    'fname' => $row->fname,
                    'lname' => $row->lname,
                    'username' => $row->username,
                    'validated' => true
                    );
            $this->session->set_userdata($data);
            return true;
        }
        // If the previous process did not validate
        // then return false.
        return false;
    }
}
?>

I can confirm the the process function is loading however any code that goes underneath the $results = $result = $this->users->validate(); doesn't appear. The model is also loading, it is as soon as I try call a function, the script kills itself.

Sorry if this question is a little bland.

Thanks Peter

Upvotes: 0

Views: 1441

Answers (2)

S&#233;rgio
S&#233;rgio

Reputation: 1607

You try capitalize the name of your file? user_model.php to User_model.php?

Upvotes: 0

Peter Stuart
Peter Stuart

Reputation: 2444

It all came down to my code. Your models class name must be equal to the name of the model file.

So in this case, I should have named my file login_model.php and then the class itself must be called Login_model (the first character must be uppercase, all other characters must be lowercase). When calling the model in the controller it must all be lower case like:

$this->load->model('login_model');

Hope this is help to anyone in the future, thanks to all for the efforts and comments :)

Upvotes: 1

Related Questions