In CodeIgniter, how do I access one model from another?

Let's say that I use the delete_user () function in the user model, and I want it to use the delete_comment () function in my comment model.

I could directly access the comment tables or load and call another model from my controller, but for my code to be as abstract as possible, I want to have access to one model from another.

Is this possible with CodeIgniter?

+3
source share
4 answers

You will need the following:

class User_model extends Model
{
    function get_something()
    {
         $CI =& get_instance();
         $CI->load->model('profile_model');
         return $CI->profile_model->get_another_thing();
    }
}
+4
source

You can simply access it as you can in the controller if it is already loaded.

$this->some_other_model->method();
0
source
   function model_load_model($model_name)
   {
      $CI =& get_instance();
      $CI->load->model($model_name);
      return $CI->$model_name;
   }
0

:

$this->load->model('comment_model');
$this->comment_model->delete_comment($userId);
0

Source: https://habr.com/ru/post/1784508/


All Articles