• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loading model inside a model

I try to load a model inside another model, but the model is not loaded in $this, but probably in $ci.
I've come up with this solution that seems more like a hack. How else can I solve this.

class MyModel extends Model {
function test() {
   $this->Another_model->insert();//$this->Another_model si not defined
   $this->_assign_libraries();//now it is
   $ci = get_instance();

Should "Another_model" be in the model object, since it lets me load it?

you can load any model in the models constructor and it will be available in evry method $this->modelname->method.

if you do not want to load the model within the constructor you have to get the CI instance

$CI =& get_instance();

Yes, but I was thinking, this is not OK, it lets me load it, but it's not there, confusing.

so this could be in a models function

$CI =& get_instance();
$returndata = $CI->testmodel->testfunction($param1,$param2);

note you do not use $this->

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.