2012-11-01 25 views
1

如何在控制器中使用模型的索引函數?Codeigniter的型號

按照CodeIgniter用戶指南中,我們知道我們可以在控制器使用

$this->name_of_model->function_of_model();  

。在重定向我們可以使用

redirect('controller_name/function_name');  

如果我們使用索引功能控制器那麼我們就可以在重定向

redirect('controller_name'); 

使用重定向到該控制器。這樣我怎麼才能使用

$this->name_of_model->function_of_model(); 

這個語法。

感謝

+1

無法理解....你想重定向到模型? – chhameed

+0

不,我想知道如果我在模型中使用索引函數,那麼我怎麼能指定,在這種語法'$ this-> name_of_model-> function_of_model();' –

+0

請解釋你的問題 –

回答

0

我們假設你正在呼籲在控制器指數模型指數現在

Class test_controller extends CI_Controller 
{ 
    function index(){ 

     $this->load->model('model_name');  
     $this->model_name->model_index();  

    } 
} 

當過您重定向到控制器指數模型指數將自動被調用。

EDITS: 如何過,你可以使用它像這樣

Class test_controller extends CI_Controller 
{ 
    function index(){ 

     $query = $this->db->get('my_table'); 
     $data['query_result'] = $query->result(); 

    } 
} 

和型號指數是這

function index() 
{ 
    $query = $this->db->get('my_table'); 
    return $query->result(); 
} 

你應該注意模型索引這裏不叫instaed模型已經沒用了,我們沒有它就做着工作。因此,分離數據庫交互是有用的,並將其與控制器相結合打破了MVC模式。

+0

我可以使用像以下 '類test_controller延伸是CI_Controller { 功能指數(){ $這 - >負載>模型( 'MODEL_NAME'); $ this-> model_name(); } }' 或 '類test_controller延伸是CI_Controller { 功能指數(){ $這 - >負載>模型( 'MODEL_NAME'); $ this-> model_index(); } }' –

+0

不,這是不可能的正確的方式是我告訴你看到我的編輯。 –