Welcome Guest, Not a member yet? Register   Sign In
Extending a model that extends MY_Model
#1

[eluser]Rob Corley[/eluser]
I'm getting the following error:-

Code:
Fatal error: Call to undefined method Some_model::method()

method() is defined in my extension to CI_Model (MY_Model).

Some_model extends Version_model and Version_model extends MY_Model. I am autoloading Version_model and I can call methods that are defined in Version_model, but not one's defined in MY_model.

This doesn't happen if I inherit directly from MY_Model.

Here's the code to clarify my explanation;

Extension to CI_Model:-

Code:
class MY_Model extends CI_Model {

function __construct()
{
  parent::__construct();
}

// --------------------------------------------------------------------

/**
  * Method
  */
public function method() {  
  return true;  
}
/* End of file MY_Model.php */
/* Location: ./application/libraries/MY_Model.php */

Version model which handles record versioning:-

Code:
class Version_model extends MY_Model {

function __construct()
{
  parent::__construct();
  
  # Some code here
}

// --------------------------------------------------------------------

/**
  * Some Version Method
  * @param none
  * @return bool true
  *
  */
private function version_method()
{
  return TRUE;
}
}
/* End of file Version_model.php */
/* Location: ./application/models/version_model.php */

The model that extends Version_model and the one that is producing the error:-

Code:
class Some_model extends Version_model {

function __construct()
{
  parent::__construct();
}
}
/* End of file Some_model.php */
/* Location: ./application/models/some_model.php */

To produce error:-

Code:
class Some_controller extends CI_Controller {

function __construct ()
{  
  parent::__construct();

  # Load some stuff
  $this->load->model('version_model');
  $this->load->model('some_model');
}

/**
  * Index page for this controller.
  */
public function index()
{  
  $this->some_model->method();
}
/* End of file some_controller.php */
/* Location: ./application/controllers/some_controller.php */

This produces the following error:-

Fatal error: Call to undefined method Some_model::method() in ...

Thanks in advance, I feel like I'm just missing something obvious...



#2

[eluser]mah0001[/eluser]
You need to place your MY_Model class in the CORE folder instead of the LIBRARIES folder.
#3

[eluser]Rob Corley[/eluser]
Thank you very much! I had already discovered this, but instead of moving the file I had copied it so I had MY_model in core and libraries and it was the lib one I was adding this new method to!

Thanks again!




Theme © iAndrew 2016 - Forum software by © MyBB