2010-03-19 8 views

Respuesta

35

Puede obtener una referencia al objeto del controlador y acceder al modelo a través de eso.

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

Otra opción es pasar el modelo al llamar a la función auxiliar.

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

Esto funciona muy bien, la pregunta es por qué es realmente necesario. Lo estoy usando ahora, pero estoy seguro de que hay mejores formas de lograr lo que estoy haciendo. ¡Gracias! – qwerty

+1

Funciona muy bien :) ¡Muchas gracias! –

Cuestiones relacionadas