开发者

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

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 cod开发者_开发问答e as abstract as possible I want to be able to access one model from within another.

Is this possible with CodeIgniter?


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();
    }
}


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

$this->some_other_model->method();


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


try to load your model like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜