dongyang2229 2011-07-05 23:52
浏览 47
已采纳

Codeigniter:如何获得加载模型的(另一个)句柄?

Suppose we have this code:

class My_controller extends CI_Controller
{
    private $model_name;

    function __construct($model_name)
    {
         $this->model_name = $model_name;
    }

    function index()
    {
         //Use the model here
    }
}

How should I create a handle to the model, to be used in the index function? I mean I don't know the name of the model, so how should I access it?

--EDIT--
Normally, we would be loading a model like this:

$this->load->model('some_model');
$this->some_model->doSomething();

Here, we know the name of the model.

Now, suppose that My_Controller is a class, which some other controllers inherit from. Each class which inherits from My_Controller, set the $model_name property. Here we don't know the name of the model, so we need a handle to that model. How should that handle be retrieved?

I hope this clarifies a bit.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dtcaw02086 2011-07-06 02:18
    关注

    You can give a model an alias.

    $this->load->model($this->model_name, '', 'some_model');
    $this->some_model->doSomething();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计