如何将模型加载到帮助器?
问题描述:
如何将模型加载到帮助程序?我需要将它加载到函数之外,但在函数中使用它们。
How can I load model to helper? I need to load it outside of functions, but use them in functions.
答
function my_helper()
{
// Get a reference to the controller object
$CI = get_instance();
// You may need to load the model if it hasn't been pre-loaded
$CI->load->model('my_model');
// Call a function of the model
$CI->my_model->do_something();
}
另一个选择是在调用辅助函数时传递模型。 p>
Another option is to pass the model in when calling the helper function.
function my_helper($my_model)
{
$my_model->do_something();
}
function my_controller_action()
{
// Call the helper function, passing in the model
my_helper($this->my_model);
}