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.
Create a new User. php file in application/controllers/ directory. Load Model_3 Model and call fun1() and fun2() methods. Print the response.
<? php $CI =& get_instance(); $CI->load->model('MyModel'); $result= $CI->MyModel->MyMethod($parameter)->result_array(); foreach($result as $row){ echo $row['column_name']; } ?>
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;.
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(); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With