在 CodeIgniter 中,如何从另一个模型访问一个模型? [英] In CodeIgniter, how do I access one model from within another?

查看:35
本文介绍了在 CodeIgniter 中,如何从另一个模型访问一个模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在用户模型中的 delete_user() 函数中,并且我希望它在我的评论模型中使用 delete_comment() 函数.

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.

这可以通过 CodeIgniter 实现吗?

Is this possible with CodeIgniter?

推荐答案

你需要这个:

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

这篇关于在 CodeIgniter 中,如何从另一个模型访问一个模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆