Code Igniter Tutorial Error - php

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');

Related

codeigniter restful api returning empty array

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.

CodeIgniter: Undefined property $db

In CodeIgniter 3.1.3 I extended CI_Model in application/core/MY_Model.php:
class MY_Model extends CI_Model {
public function __construct($id = NULL) {
parent::__construct();
$this->load($id);
}
public function load($id) {
$this->db->where('id', $id);
$this->db->limit(1);
$query = $this->db->query($this->_table);
if ($row = $query->result()) {
// #todo Process results
}
// Free the resources.
$query->free_result();
}
}
My User_model looks like this:
class User_model extends MY_Model {
public function __construct($id = NULL) {
parent::__construct($id);
}
}
I also extended the CI_Controller in application/core/MY_Controller as follows:
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('User_model');
}
}
I autoloaded the database connection in application/config/autoload.php as:
$autoload['libraries'] = array('database');
Without loading the User_model in the controller I was able to run migrations, so the database connection is configured correctly. But when I added $this->load->model('User_model') I get the error "Undefined property: User_model::$db".
If I let User_model extend CI_Model it runs without errors and with a var_dump in the homepage's controller it shows that the database is autoloaded correctly. But as soon as I put MY_Model in between, the database class is undefined in the model and also $this->load in the model returns NULL, so it appears the model is not properly constructed.
I can only imagine this to be a very small mistake, but I've been staring at it for hours with several breaks in between and I just don't see it. Can anyone else help me?
From a controller, lets say A_controller
$this->load->library('MyLib');
In MyLib, my original issue is this block
$ci = &get_instance();
$ci->load->model('my_model');
$this->active_model = $ci->my_model;
Then the miracle come when it become
$ci = &get_instance();
$ci->load->model('my_model');
$ci->my_model->db = & $ci->db;
$this->basic_model = $ci->my_model;
When CI loads a model $db property come along with CI.
So, when you call $this->db in model which means you want "my_model" have the property, right?
What I did just link the property from CI to my_model!!!

codeigniter global array for a model

In my controller I want to declare a global variable which will always fetch all areas from my db
currently I'm passing $data['dropdowns']
in all methods in my class
{
$data['dropdowns']=loading some other model method
$this->load->view('commons/header',$data);
}
{
$data['dropdowns']=loading some other model metod
$this->load->view('commons/header',$data);
}
{
$data['dropdowns']=loading some other model metod
$this->load->view('commons/header',$data);
}
{
$data['dropdowns']=loading some other model metod
$this->load->view('commons/header',$data);
}
the thing is I want to now send $data['area'] to all the views without having to declare it again and again in each method
$data['area']= $this->area_model->get_all_locations();
You want to add global variable , but as per my suggest to use global function to use any where to using to send parameter, so please check below my code.
Note : please load your model in application/config/autoload.php file
This is simple demo :
controller
{
$data['dropdowns']=$this->your_model_name->get_records('table_name','select field like id, name');
$this->load->view('commons/header',$data);
}
{
$data['dropdowns']=$this->your_model_name->get_records('table_name','select field like id, name,user_name');
$this->load->view('commons/header',$data);
}
Your model
function get_records($table_name,$field_name)
{
$this->db->select("$field_name");
$this->db->from("$table_name");
$query=$this->db->get();
return $query->result_array();
}
create a base_controller and placed in application/core
class base_controller extends CI_Controller
{
public $area = array();
function __construct()
{
// Call the Controller constructor
parent::__construct();
$this->get_area();
}
public function get_area() {
$this->load->model('area_model');
$this->area= $this->area_model->get_all_locations();
}
}
now $this->area is available in all controller which extends base_controller and all common functionality you can put here
class homepage extends base_controller{
function __construct()
{
// Call the Controller constructor
parent::__construct();
}
public function index()
{
$data = $this->area; // call this wherever u need
$this->load->view('commons/header',$data);
}
}
importantly $this->area; one can use directly inside view
Create a helper for your custom functions
Eg: custom_helper.php then load your custom helper in autoload.php
In custom_helper.php, create a function for getting area.
if (!function_exists('get_area')) {
function get_area() {
$CI = & get_instance();
$area= $CI->area_model->get_all_locations();
return $area;
}
}
You can call get_area() in your views without declaring in controllers..

CodeIgniter - constructors in models

So here is my controller:
class Search extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('search_model');
$this->search_model->search_result = $_POST;
}
public function index()
{
$data['results'] = $this->search_model->get_results();
$this->load->view('search_results', $data);
}
And here is my model:
class Search_model extends CI_Model {
protected $search_query;
function __construct($search_query)
{
parent::__construct();
$this->load->database();
$this->search_query = $search_query;
}
But this doesn't seem to work. What I want to do is pass the posted form ($_POST) to my model, then do stuff with it. But it seems messy to pass $_POST to each method of my model. My plan is to extract the variables sent with $_POST and construct these as properties such as $website_url, $text_query etc..., then call these in methods with $this->website_url;
I'm relatively new to CodeIgniter so just getting to grips with the basics
for your special purpose you can try this code
Controller:
class Search extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('search_model');
$this->init();
}
private function init()
{
$this->search_model->init( $this->input->post() );
}
public function index()
{
$data['results'] = $this->search_model->get_results();
$this->load->view('search_results', $data);
}
model:
class Search_model extends CI_Model {
protected $search_query;
function __construct()
{
parent::__construct();
$this->load->database();
}
public function init( $search_query )
{
$this->search_query = $search_query;
}
you have protected $search_query; which you can't access it from your controller.
You either have to change it to public or create getter and setter for it. or just getter depending on your domain/business logic.
And it should have been obvious as you should get an error saying
Fatal error: Cannot access protected property in file some/path/to/file!
Don't put the 'search query' in your model constructor.
Controller:
class Search extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('search_model');
}
public function index()
{
if ($this->input->server('REQUEST_METHOD') == 'POST')
{
// you should probably validate/clean the post data instead
// of sending it straight to the model
$results = $this->search_model->get_where($_POST);
}
else
{
// if it's not a post, you probably need something...
// either here, or somewhere in your view to handle empty data
$results = array();
}
$data['results'] = $results
$this->load->view('search_results', $data);
}
Your Model:
class Search_model extends CI_Model {
function __construct()
{
parent::__construct();
$this->load->database(); // <--- you may want to autoload 'database' library
}
function get_where($where)
{
$this->db->where($where);
// add select, order, joins, etc here
return $this->db->get('mytable'); // whatever your table name is
}

Codeigniter controller model call fail

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

Categories