2010-03-19 11 views

risposta

35

È possibile ottenere un riferimento all'oggetto controller e accedere al modello attraverso questo.

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

Un'altra opzione è passare il modello quando si chiama la funzione di supporto.

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

Questo funziona benissimo, la domanda è perché è davvero necessario. Lo sto usando ora, ma sono sicuro che ci sono modi migliori per ottenere ciò che sto facendo. Grazie comunque! – qwerty

+1

Funziona alla grande :) Grazie mille! –

Problemi correlati