开发者

How can I load model to helper?

开发者 https://www.devze.com 2022-12-24 01:21 出处:网络
How can I load model to helper? I need to load it outside of functions, but use开发者_运维问答 them in functions.You could get a reference to the controller object and access the model through that.

How can I load model to helper? I need to load it outside of functions, but use开发者_运维问答 them in functions.


You could get a reference to the controller object and access the model through that.

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

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

精彩评论

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

关注公众号