Search code examples
codeigniterpropertiescallmodelsmember

Undefined property: Users::$Usersa Fatal error: Call to a member function insert() on a non-object


I'm using codeigniter framework and I'm getting this error when I've submitted my form:

Message: Undefined property: Users::$Usersa
Filename: controllers/users.php
Line: 44
Fatal error: Call to a member function insert() on a non-object

Users Controller (section that's broken):

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

$register = array(
        'username' => $this->input->post('username'),
        'password' => $this->input->post('password'),
        'email' => $this->input->post('email')
    );

    // validation stuff

    if ($this->input->post())
        if ($this->form_validation->run() == FALSE) {

        } else {
            $this->Usersa->insert($register); // This is line 44
        }

Usersa Model (this file is located in application/models)

class Usersa extends MY_Model {

var $table = 'users';

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

MY_Model (located in applications/core)

class MY_Model extends CI_Model{

protected $table = '';


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

function insert() {
    $this->db->insert($this->table, $register);

}
}

The table data is getting passed through fine and I'm pretty sure the register data is as well I just can't seem to figure out why I'm getting the error message.


Solution

  • You are loading the model as "usera" - but referencing it as $this->Usera - you should references it lowercased:

    $this->usersa->insert($register);

    Alternately, you can pass the name you'd like to reference the model at with the 2nd parameter as illustrated @ http://codeigniter.com/user_guide/general/models.html:

    $this->load->model('usersa','some_model_name');

    $this->some_model_name->insert(...);