CodeIgniter Forums
FIXED: Loading a model from inside another model. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: FIXED: Loading a model from inside another model. (/showthread.php?tid=8857)



FIXED: Loading a model from inside another model. - El Forum - 06-03-2008

[eluser]louis w[/eluser]
In some cases, I need my model to load another model and fetch some info before returning it all to the controller.

The name of the sub-model needs to be dynamic, as it will be coming from the db row.

This is the code I am trying in my model.

Code:
if (!is_object('$this->'.$module.'_model'))
     $this->load->model($module.'_model');

$module_item = $this->{$module.'_model'}->getByID($row[$this->id_field]);

I get this error:
Message: Undefined property: A_model::$b_model (i changed the variable names)


This was developed under PHP5 and I do not care about backwards compatibility with 4.


FIXED: Loading a model from inside another model. - El Forum - 06-03-2008

[eluser]louis w[/eluser]
Oh yes, hello obvious. Nice to meet you.

I forgot that $this-> does not reference the CI instance in a model. Fixed with good old &get;_instance();


FIXED: Loading a model from inside another model. - El Forum - 06-03-2008

[eluser]stuffradio[/eluser]
Glad it worked out!