Reputation: 757
I am working with CodeIgniter 3.0.5 and I have done a fair share of research to find the solution to my relatively simple to solve problem.
Just have a look at my case.
I have a model with file name Technology_model.php
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Technology_Model extends CI_Model
{
function __construct()
{
parent::__construct();
}
public function getData()
{
$query = $this->db->get('technology');
return $query->result();
}
}
?>
I've loaded this Model in my controller file Technology.php
and I'm trying to access the methods in my model.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Technology extends CI_Controller
{
// *******Constructor*******
function __construct()
{
parent::__construct();
}
// *******Constructor*******
public function index()
{
$this->load->model('Technology_Model');
//Model loads fine..checked
$data['records'] =
$this->Technology_model->getData(); //*** <=ERROR in this line
}
}
?>
The solutions I found were all about changing the character casing of the following:
I have tried all the Naming conventions. The model is getting loaded. But when I try to call the methods in the model, I get the following errors.
Some say its a Bug in CI 3.0.5. I have configured my database, loaded database library in autoload.php and tried all solutions provided, though they didn't work out.
Please help me find a solution. Thanks. These are the errors I get
The models should be named, as in my case, Technology_model.php . Also the respective class for this model would be Technology_Model
Upvotes: 1
Views: 1732
Reputation: 7111
I agree with @DFriend 's answer, although I am using some kind of distinctive variable naming personal standardization in a way with no problems:
File name:
Technology_m.php // sparing few chars
Class name:
Technology_m
Loader method argument:
$this->load->model('Technology_m');
Approaching model method/property:
$this->Technology_m->getData();
Thing is in my internal systematisation and variable convention since you can call model with ucfirst()
but not library itself. I use to have models with ucfirst() in all calls in my controller respecting model convention as I described above, but also I use to have library call from controller that follows strtolower()
standard. That way, seeing the code snippet on first sight, I am able to determine if something is model or library (instance/property/method).
Upvotes: 0
Reputation: 1746
It looks like CI basically cannot find your model's PHP file and therefore cannot call getData()
on what it thinks is a non-existent class. You haven't given us CI's directory structure, but AFAIK depending on where your model is, then the path to its PHP file needs to reflect that.
For example, my own admin models are in: Codeigniter/application/models/admin. So if I need to call an admin model from a controller I do it like this:
$this->load->model('admin/my_model', 'MyAdmin', true);
$data = $this->MyAdmin->get();
Note that relative path to my model dir in the first param and also the 2nd and param which declares a variable on which you can run your model's method(s) (Optional, just using it as an example).
Upvotes: 0
Reputation: 8964
When you use $this->load->model()
capitalization does not matter with regard to finding the model class - Technology_Model
will work as will technology_model
. However, the string used will be the name of the instance of the class and IS case sensitive - like any other PHP var.
In other words, if you do this:
$this->load->model('Technology_Model');
then to access methods of that class instance you need to use the same (case sensitive) word ie,
$this->Technology_Model->getData();
Not using the exact same string results in the error you have been seeing.
All that said, my practice is to use all lowercase for instance names. For file and class names only the first character is uppercase. So the file would be Technology_model.php
, with a class definition of class Technology_model extends CI_Model
.
It would be loaded in a controller with $this->load->model('technology_model');
Upvotes: 1