2014-03-07 54 views
0

我有這個模型在我的笨設置:笨的RESTful API - 如何獲得參數

function get_all_artists(){ 

    $this->db->select('artist_id, artist_name'); 
    $this->db->from('artists'); 
    return $this->db->get();  
} 

如何抓在我的Rest_Controller設置這些參數? _get()功能將如何顯示?

public function artists_get() 
{ 
??? 
} 

在此先感謝

+0

你要訪問返回的結果所以$this->db->get()->result_array(); 從模型中檢索數據'get_all_artists()'函數? –

+0

你的意思是從你的控制器調用該函數? $這 - >負載>模型( 'MODEL_NAME'); '$ artists = $ this-> model_name-> get_all_artists(); – Dan

+0

是的,我需要從結果 – SHT

回答

1

嘗試

public function artists_get(){ 
    $this->load->model('your_model_name');// load model here 
    $res = $this->your_model_name->get_all_artists(); 
    $result = $res->result_array();// get result as associative array 
    return json_encode($result);// encode with json 
} 

有了這個庫,你需要附加的方法類型函數名。對於GET,_post對於POST_put對於PUT,_delete對於DELETE。例如functionname_get()

0

正如在另一個答案中指出的,你會想要檢索結果作爲一個數組。

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

$artists = $this->model_name->get_all_artists(); 

如果你想JSON發送到前端,那麼你需要做這樣的事情:

$vars['artists'] = json_encode($artists); 

$this->_getTemplate('default')->build('artists_page', $vars);