Codeigniter Extend custom model produces FATAL ERROR

I have seen several different posts, but none of them seem to work 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 get the following error message:

 PHP Fatal error: Class 'Users_Model' not found in /Users/rosswilson/Localhost/thedrumrooms/dreamweaver/ci/application/models/students_model.php 

I used require_once to include the file in the extended class, and it works. Is this the best way / right way to do this?

Thanks.

+4
source share
3 answers

The standard practice in CI is to create a base model inside application/core called MY_Model.php , MY_ depends on what is defined in your configuration.

Then inside your MY_Model.php you can have many classes defined in this file that you can extend in your model, basically the CodeIgniter download model looks for specific classes in this path and file.

But if you want to use require_once , you have to use APPPATH (application path) as defined in index.php . But your custom model will still have to extend CI_Model , since it uses the main class for the CI model, or your model will not work.

ex require_once APPPATH.'/models/test.php';

ADDED NOTES:

Thank you @dean Or for pointing this out. Inside the MY_Model.php file there should be a MY_Model class that extends CI_Model .

+5
source

Are you sure that Users_model is loaded before loading Students_model? you can do this in the autoload.php configuration file.

+1
source

Place the users_model.php file in the main directory. Then try to inherit Users_Model in Students_Model.

0
source

Source: https://habr.com/ru/post/1487444/


All Articles