I am trying to fetch items by id but instead its returning null array.The code works fine when I fetch all the items.
Here is the controller:
class Search extends REST_Controller
{
function __construct()
{
// Construct the parent class
parent::__construct();
$this->load->model('Courses_model');
$this->load->model('Courses_category_model');
$this->load->model('Courses_lessons_model');
$this->load->model('Courses_photos_model');
$this->load->model('Providers_model');
}
function index_get()
{
$category_id = $this->get('category_id');
$result = $this->Courses_model->get_courses_category($category_id);
$this->response([
'status' => TRUE,
'data' => $result
], REST_Controller::HTTP_OK);
}
}
and the model file is here and the model file is here:
class Courses_model extends CI_Model
{
public function __construct()
{
// Call the CI_Model constructor
parent::__construct();
}
public function get_courses_category($category_id)
{
$this->db->where('category_id', $category_id);
$query = $this->db->get('courses');
return $query->result();
}
i have seem u r question and find out ans please check the below link i think help u.
https://github.com/chriskacerguis/codeigniter-restserver/blob/master/application/libraries/REST_Controller.php
RestApi Post request using CodeIgniter
hahaa. Finally, after a little snap I solved this. I just defined the routes and it worked like a charm.
Related
I created a controller named Api.php then I extended the Rest_Controller. I noticed that I can only use index_get() when creating a function in this controller
<?php
class Api extends REST_Controller{
public function __construct()
{
parent::__construct();
}
public function index_get(){
$car_id = $this->get('car_id');
if(!$car_id){
$this->response("No Car ID specified", 400);
exit;
}
$result = $this->model_getvalues->getCars( $car_id );
if($result){
$this->response($result, 200);
exit;
}
else{
$this->response("Invalid Car ID", 404);
exit;
}
}
}
but when I try creating my desired function like getAllCars() instead of index_get() I get an error message telling me unknown function.
How can I define my own function instead of using index_get() when using rest api library in CodeIgniter?
Thanks i have been able to figure it out, i just found out that the name before the _get is what matters to the url i.e when one has a method like getCars_get, you will have to call it using just getCars without the _get attach to it, it work for me. it means that one can have more than _get method in the API controller.
By default on https://github.com/chriskacerguis/codeigniter-restserver#handling-requests the method is index_get(),another way to use your own method is play with the HTTP GET parameter
ex:
if($this->get('car_id') == 'all'){
//your own function here
}
Or if you really want to create your own method you can refer on this http://programmerblog.net/create-restful-web-services-in-codeigniter/
All you have to do is change function index_get() to getAllCars_get(),
`<?php
class Api extends REST_Controller{
public function __construct()
{
parent::__construct();
}
public function getAllCars_get(){
//your code
}
}
?>
`
Like this
This is the code for my controller:
class products extends CI_Controller {
public function index() {
$this->load->model('getproducts');
$this->load->view('productlist');
}
}
?>
When I load this, it returns a blank page, however when I comment out the line that loads the model, it returns the view as expected (pointing to an error in the model). Here is the code for the model:
<?php
class getproducts extends Model {
function listProducts() {
$query = $this->db->get('ProjectProducts');
if ($query->num_rows() > 0) {
foreach ($query->result() as $row {
$productdata[] = $row;
}
return $productdata;
}
}
}
?>
Am I missing something obvious here? I'm not even using any of the data from the model yet.
What version you use of codeigniter
please note that you should extends CI_Model not model
class getproducts extends CI_Model {
Please make sure your controller start letter be capital ie. upper
case and extend it as CI_Model
you are missing
function __construct()
{
parent::__construct();
}
and this code before your function listProducts and after class starting hopefully this will resolve your issue.
and also use same function in your controller as well
When i try to write and run the Code Igniter tutorial, it throws this error:
Call to undefined method News_model::get_news() in application\controllers\news.php on line 21
Here is line 21:
$data['news'] = $this->news_model->get_news($slug);
mews model
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this ->db->get('news')
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
}
If you use your model in controller - you must write this code
$this->load->model('News_model');
$data['news'] = $this->News_model->get_news($slug);
And check line
parent::__construct();
in your controller __construct method.
(PHP does not instantiate the parent constructor automatically if the child defines a constructor, unless the child specifically instantiates the parent's constructor)
If you use model from another side your application you must write this code
$CI = &get_instance();
$CI->load->model('News_model');
$data['news'] = $CI->News_model->get_news($slug);
Without posting more code it looks like there are two things to check here.
First, you have to call the parent constructor in your model. So your News_model constructor should look like this:
function __construct()
{
parent::__construct();
$this->load->database();
}
Second, if the code for line 21 you posted above is correct, the model name has to match your class name. So line 21 should read:
$data['news'] = $this->News_model->get_news($slug);
Note the uppercase 'N' in News_model.
This is essential what yAnTar wrote but phrased differently. Hopefully you'll understand either one of them.
may this will help you
1.In your controller:-
parent::__construct();
$this->load->model('News_model ');
$data['news'] = $this->news_model->get_news($slug);
2.In Model:-
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news($slug)
{
if ($slug)
{
$query = $this ->db->get('news')
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
}
It seems model not loaded.
You have to load model before using it
Either in controller or in autoload array
In controller load model using
$this->load->model('model_name');
And in your case
$this->load->model('news_model');
I am new to CI and I want to update some data in mysql . So here is my controller
class Ci_update extends CI_Controller
{
function __construct() {
parent::__construct();
}
function index()
{
$data = array
(
'title' => 'Data Structure using C',
'text' => 'Data Structure Using C, for, IIIrd Sem VTU CSE students'
);
$id = 4 ;
$this->load->model('ci_update_model');
$this->ci_update_model($data,$id);
}
}
and my model is :
class Ci_update_model extends CI_Model
{
function __construct() {
parent::__construct();
}
function updateData($data,$id)
{
$this->db->where('id',$id);
$this->db->update('data',$data);
}
}
But when I tried to run the program , it says Call to undefined method Ci_update::ci_update_model() in C:\wamp\www\ci\application\controllers\ci_update.php on line 19
What wrom am I doing?
Use as below
$this->load->model('ci_update_model');
$this->ci_update_model->updateData($data,$id);
DO this changes in your Controller code,Rest is same:
$this->load->model('ci_update_model');
$this->ci_update_model->updateData($data,$id);
in your controller's constructer add this line
$this->load->model('Ci_update_model');
and the error will get resolved
I am doing an ajax call..which doesnt output any mysql errors..Basically, I am trying to delete two records...
public function task_delete($task_id)
{
$this->load->database();
$this->db->delete('task_pages', array('id' => $task_id));
$this->db->delete('tasks', array('id' => $task_id));
}
This is the model..
And this is the controller..
class Ajax extends CI_Controller {
public function delete()
{
$this->load->database();
$this->load->model('tasks_model','task_delete');
$result=$this->task_delete-> task_delete($this->input->post('myId'));
echo $result;
}
}
Where does my code fails?
UPDATE:
I get an error..the method deleteTask is being called on none object
Assuming your model is also called task_delete and the function you want to call is deleteTask, then you have given the function in your model the wrong name, it should be declared as this:
public function deleteTask($task_id)