In CodeIgniter, how do I access one model from wit

2019-01-23 21:42发布

问题:

Say I am in the delete_user() function in the user model, and I want it to use the delete_comment() function in my comment model.

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

Is this possible with CodeIgniter?

回答1:

You'd need this:

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


回答2:

You can simply access it like you can in a controller given it's already loaded.

$this->some_other_model->method();


回答3:

   function model_load_model($model_name)
   {
      $CI =& get_instance();
      $CI->load->model($model_name);
      return $CI->$model_name;
   }


回答4:

try to load your model like this:

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