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

#1
[eluser]asprillia[/eluser]
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.

example:
Code:
<?
class MyModel extends Model {
function test() {
   $this->load->model('Another_model');
   $this->Another_model->insert();//$this->Another_model si not defined
   $this->_assign_libraries();//now it is
   $this->Another_model->insert();//works
//or
   $ci = get_instance();
   $ci->Another_model->insert();
}

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

#2
[eluser]gtech[/eluser]
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();
$CI->load->model('LocationType');

#3
[eluser]asprillia[/eluser]
Yes, but I was thinking, this is not OK, it lets me load it, but it's not there, confusing.

#4
[eluser]gtech[/eluser]
so this could be in a models function

Code:
...
$CI =& get_instance();
$CI->load->model('testmodel');
$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.