I've seen a few different posts on this, but none of them seem to be working for me.
I have one class that extends CI_Model:
class Users_Model extends CI_Model {
public function __construct(){
parent::__construct();
}
and then:
class Students_Model extends Users_Model {
private $_students;
public function __construct(){
parent::__construct();
$this->_students = $this->get_students();
}
However I then receive this error message:
PHP Fatal error: Class 'Users_Model' not found in /Users/rosswilson/Localhost/thedrumrooms/dreamweaver/ci/application/models/students_model.php
I've used require_once
to include the file in the extended class and it works.
Is this the best practice/correct way to do this?
Thanks.
Put the file users_model.php in the core directory. Then try inheriting Users_Model into Students_Model.
did you make sure that Users_model is loaded before the Students_model was loaded? you can do this in the autoload.php config file.
The standard practice in CI is to create a Base Model inside
application/core
namedMY_Model.php
,MY_
depends on what is defined in your config.Then inside your
MY_Model.php
you can have many classes defined in that file that you can extend in your Model, basically CodeIgniter's load model looks for defined classes on this path and file.But if you want to use
require_once
you have to use theAPPPATH
(application path) as defined onindex.php
. But your custom model would still have to extendCI_Model
as it uses the core class for CI Model or your model will not work.ex
require_once APPPATH.'/models/test.php';
ADDED NOTES:
thank you @dean Or for pointing that out. Inside the
MY_Model.php
file there must be aMY_Model class
that Extends theCI_Model
.The solution is to include the parent models's definition file in the derived class first.
Users_Model
Students_Model
Test case
include somewhere
Result
Screenshot
Addendum
In order to get more structure in your design, you may decide to put those 2 classes to a subfolder
models/people
. You then modify as follows: