Can one call a method from another model in a model in Code Igniter?

Go To StackoverFlow.com

19

Can one call a method from another model in a model in Code Igniter? I tried it out, and it seemed to work after I autoloaded all of my models.

However, does ordering of the models matter? Just because it worked for me once does not mean it works all of the time.

2012-04-05 01:22
by John Hoffman


32

Yes, you can call a method from another model in a model in Code Igniter. You need the model you are calling a method on to be loaded. If you autoload all your models, it will always work. The order of autoloading does not matter.

When I want to call a method on another model, I usually load that model before using it. E.g.:

class User_model extends CI_Model
{
  function test()
  {
    $this->load->model('Blog_model', 'blog');
    $result = $this->blog->method_on_blog_model();
  }
}
2012-04-05 01:57
by Mischa
Absolutely correct its really usefull to me! - Darshan ambaliya 2016-08-15 07:55
Ads