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.
The standard practice in CI is to create a Base Model inside application/core
named MY_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 the APPPATH
(application path) as defined on index.php
. But your custom model would still have to extend CI_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 a MY_Model class
that Extends the CI_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 solution is to include the parent models's definition file in the derived class first.
Users_Model
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Users_Model extends CI_Model {
public function __construct(){
echo("<hr><pre> L: ".__LINE__." :: :: F: ".__FILE__ . ' M: '.__METHOD__ . ' ' . date('H:i:s').' ( ) '."</pre>\n" . get_class($this) );
}
}
/* End of file Users_Model.php */
/* Location: ./application/models/Users_Model.php */
Students_Model
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
include_once(APPPATH . 'models/Users_Model.php');
class Students_Model extends Users_Model {
private $_students;
public function __construct(){
parent::__construct();
echo("<hr><pre> L: ".__LINE__." :: :: F: ".__FILE__ . ' M: '.__METHOD__ . ' ' . date('H:i:s').' ( ) '."</pre>\n" . get_class($this));
}
}
/* End of file Students_Model.php */
/* Location: ./application/models/Students_Model.php */
Test case
include somewhere
$this->load->model('Students_Model');
Result
L: 9 :: :: F: /www/application/models/Users_Model.php M: Users_Model::__construct 23:16:34 ( )
Students_Model
L: 14 :: :: F: /www/application/models/Students_Model.php M: Students_Model::__construct 23:16:34 ( )
Students_Model
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:
include_once(APPPATH . 'models/people/Users_Model.php');
$this->load->model('people/Students_Model');
Put the file users_model.php in the core directory. Then try inheriting Users_Model into Students_Model.