I created a new model class as my_model.php inside /models folder, and a function inside it to load all elements:
function get_all(){
$query = $this->db->get('test'); //test is my table
return $query->result();
}
In the controller, I instantiated the class and called the method;
$this->load->model('my_model');
$res = $this->my_model->get_all();
But, this throws me error saying:
Fatal error: Call to a member function get() on a non-object in /var/www/testapp/application/models/my_model.php on line 7
This line 7 points to the portion of the code where I have used $this->db. I tried to see the value of $db but I think it is magic accessor __get and __set, so I could not see the value of this property before calling that method.
I tried googling for several other cases but none of them match my scenarios and rather none of them could solve my problem.
You have to load the Database first
So, all code:
Or, load database in your
__constructmethod.Or, IMO, It’s better to autoload database by changing
application/config/autoload.php, example is below.