Как загрузить модель в помощник?

Как загрузить модель в помощник? Мне нужно загрузить его за пределы функций, но использовать их в функциях.

Ответы

Ответ 1

Вы можете получить ссылку на объект контроллера и получить доступ к модели через это.

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

Другой вариант - передать модель при вызове вспомогательной функции.

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