开发者

Codeigniter: How to obtain a(nother) handle to a loaded model?

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.


You can give a model an alias.

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜