Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can one call a method from another model in a model in CodeIgniter?

Tags:

Can one call a method from another model in a model in CodeIgniter? 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.

like image 500
John Hoffman Avatar asked Apr 05 '12 01:04

John Hoffman


People also ask

How do you call a method from another model?

Create a new User. php file in application/controllers/ directory. Load Model_3 Model and call fun1() and fun2() methods. Print the response.

Can we call model from view in CodeIgniter?

<? php $CI =& get_instance(); $CI->load->model('MyModel'); $result= $CI->MyModel->MyMethod($parameter)->result_array(); foreach($result as $row){ echo $row['column_name']; } ?>

How do you call a model function in controller in CI?

To call the model method on an object created by the new keyword on user model you have to include user model in your controller file by using the use keyword like use App\Models\UserModel;.


1 Answers

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();   } } 
like image 193
Mischa Avatar answered Sep 22 '22 05:09

Mischa