Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter Extend custom model produces FATAL ERROR

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.

like image 242
Rwd Avatar asked Mar 23 '23 11:03

Rwd


2 Answers

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.

like image 122
tomexsans Avatar answered Apr 01 '23 08:04

tomexsans


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.

like image 25
pgee70 Avatar answered Apr 01 '23 06:04

pgee70