2010-03-19 55 views

回答

35

您可以獲得對控制器對象的引用並通過該對象訪問模型。

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

這很好,問題是爲什麼它真的需要。我現在正在使用它,但我相信有更好的方法來實現我所做的。雖然謝謝! – qwerty 2012-04-19 18:19:16

+1

很棒:)非常感謝! – 2013-06-05 20:12:48

相關問題