Reputation: 720
I have this model in my Codeigniter setup:
function get_all_artists(){
$this->db->select('artist_id, artist_name');
$this->db->from('artists');
return $this->db->get();
}
How do I fetch those parameters in my Rest_Controller setup? How would the _get()
-function look like?
public function artists_get()
{
???
}
Thanks in advance
Upvotes: 1
Views: 2736
Reputation: 1304
If you are using the REST EXTENSION for CodeIgniter then you must create an object and invoke the method "Respose".
public function artists_get() {
$response = array(
'status'=>TRUE,
'data'=>$result, // This can be an object or array
'obs'=>'all',
);
$this->response($response);
}
That's the official way to do it... and it works perfectly!
Upvotes: 0
Reputation: 6344
Try
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
}
With this library you need to append the method type to the function name. _get
forGET
, _post
for POST
, _put
for PUT
, _delete
for DELETE
. e.g. functionname_get()
.
Upvotes: 1
Reputation: 9468
As noted in another answer you'll want to retrieve results as an array. So $this->db->get()->result_array();
To retrieve data from the model:
$this->load->model('model_name');
$artists = $this->model_name->get_all_artists();
If you want to send json to front end then you need to do something like this:
$vars['artists'] = json_encode($artists);
$this->_getTemplate('default')->build('artists_page', $vars);
Upvotes: 0