Getting error Call to undefined method CourseModel::deletecour() in Codeigniter? - php

Hello developers i am getting error but don't know why? All thing is correct i think so.
I am doing just simple delete in codeigniter using id.
Just check my code.
This is my controller.
public function deletecourse(){
$id = $this->uri->segment(4);
$elete = array(
'table'=>'ls_courses',
'where'=>array('id'=>$id)
);
$result=$this->coursemodel->deletecour($elete);
if($result){
$this->session->set_flashdata('success', 'Successfully deleted.');
}
else{
$this->session->set_flashdata('warning', 'Unable to delete data.Please try again.');
}
redirect(base_url().'admin/course/index/');
}
This is Model function.
class CourseModel extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function deletecour($elete)
{
$this->db->where($elete['where']);
return $this->db->delete($elete['table']);
}
public function fetchCourseByParentID($id)
{
$this->db->select('course_name, image, sort_order, parent_id,
date_added, status');
$this->db->from('ls_courses');
$this->db->where('id = ' . $id);
$query = $this->db->get();
if(!empty($query->result_array()))
{
return $query->result_array();
}
else
{
return $this->db->error();
}
}
}

Hello Guys first of all thank you because you all replied very frequently to my question. I found the problem of my code.
In my autoload.php i had passed the
$autoload['model'] = array('coursemodel','admin/coursemodel');
These are two different model but one for website and 2nd for admin side.
So when i was calling the model name. Problem is here with my model name which was clashing or you can say outside model was calling which is not for admin and there was no method named such as deletecour($elete).
So, that's why error was coming.
Thanks for your replies and sorry for wasting your time for this silly mistake.
$this->coursemodel->deletecour($elete);

at your model ...
name of your class is camel case.. in codeigniter documentation .. i have read somewhere to use underscore for naming classes with first later as capital ... change your model name from CourseModel to Course_model and try...

i dont know why it is happening ... but try to get all method available in your class... so you can find that ... why you are not able to call your method at model...
get all method available in your model
class CourseModel extends CI_Model
{
public function __construct()
{
parent::__construct();
var_dump(get_class_methods($this));
EXIT;
}
}

Related

How to fix Codeigniter error: Call to a member function on null?

It's my first time exploring codeigniter and I've been trying to create a login where if users are not found in the database, it will redirect back to the login screen. I have not been successful because I've been getting this error:
first error message
Followed by
second error message
I've tried loading the database and session inside function verify() using
$this->load->database();
$this->load->session();
but even so, I didn't think it would work since I already declared database and session in autoload.php
$autoload['libraries'] = array('database', 'session');
Here's the controllers/Login.php
class Login extends CI_Controller{
function index(){
$this->load->view("templates/_header");
$this->load->view("login");
$this->load->view("templates/_footer");
$this->load->view("templates/_scripts");
}
function verify(){
$this->load->model('user');
$check = $this->model->validate();
if($check){
} else {
redirect('login');
}
}
}
Here's the models/User.php
<?php
class User extends CI_Model{
function validate(){
$arr['username'] = $this->input->post('username');
$arr['password'] = password_hash($this->input->post('password', PASSWORD_DEFAULT));
return $this->db->get_where('users', $arr)->row();
}
}
What do you guys think is the problem with it?
you have load your model true way but to use it you have to call its class name (here is User) to work work correctly.
note: if you want to code in a clean way and don't conflict them try to name your models like User_model or User_m, so if you have a controller with name of User its not conflict with your model.
:-)

How can I define my function when using REST API in CodeIgniter?

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

Where should I define a custom function in CodeIgniter and how can I use it in every page?

I'm a newbie in CodeIgniter. I want to use the following function and needs to be run in every page. I'm calling the function in the top header of the view. Even though different subviews are loaded in the content below, the navigation bar should load this function in order to count notifications and messages on every run.
function count_records_in_table($table, $where)
{
$this->db->select('count(id) as rows');
$this->db->from($table);
$this->db->where($where);
$query = $this->db->get();
foreach($query->result() as $r)
{
return $r->rows;
}
}
first create the common model
then put this function on it and load this model in
$autoload['model'] = array('mdl_common');
autoload file in config
finally you can use your function by calling model common like this
$this->mdl_common->count_records_in_table("table_name","where");
you can use in every page you want try it it will helps .
Here is suitable solution
First create one file say common.php in application/libraries/common.php
Now write below code in common.php file
class Common extends CI_Controller
{
public function customFailMessage($msg)
{
$xml = new SimpleXMLElement('<Response/>');
$xml->addChild('status','success');
$xml->addChild('msg',"my message");
print ($xml->asXML());
die;
}
}
Now call you function from any of your controller like this
class Ws_plan extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('common');
}
public function test()
{
$this->common->customFailMessage();
}
}
Hope you got idea how to use common function in all controller

Cakephp error:Call to a member function find() on a non-object

I am having problems with my code. When I try to debug my web application I get the following error message....Call to a member function find() on a non-object.....here is my code
class TeamsController extends AppController {
var $name = 'Teams';
function index() {
$this->set('teams', $this->team->find('all'));
}
function Welcome() {
}
}
I am trying to display records from my MySQL database. Now with that said, I did this tutorial and I followed the instructions down to the tee.....but somehow my code has bugs. The only difference between my code and the code of the tutorial I did is the variable names...and the controller names....and the I dont have the hello world function... Here is a sample of the code from the tutorial I did....
class PostsController extends AppController {
var $name = 'Posts';
function index() {
$this->set('posts', $this->Post->find('all'));
}
function hello_world() {
}
}
With that said, am I suppose to declare an instance of an object to get this to work?
It's likely a case sensitivity issue:
function index() {
$this->set('teams', $this->Team->find('all'));
}
If not, ensure your controller has access to the Teams model (e.g. $uses).

CodeIgniter Undefined property when loading a model [duplicate]

This question already has answers here:
Codeigniter - I am looking to use/connect to a different database for one of my controllers and one model
(3 answers)
Closed 10 years ago.
I'm receiving the following errors whilst trying to implement a very simple model controller in codeigniter. I'm new to the framework but as far as I can see this should work.
I've also tried auto loading the model. I am autoloading the database library.
Message: Undefined property: User::$user_model
Fatal error: Call to a member function get_user() on a non-object
The model
class User_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
function get_user()
{
return "test";
}
}
The controller
class User extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
function index()
{
$this->load->model('User_model');
$data['value'] = $this->User_model->get_user();
$this->load->view('user_edit', $data);
}
}
Thanks
just use some thing like this, $this->load->model('user_model'); don't use $this->load->model('User_model'); and make sure you have same name, when the model name like user_mode.php and the class like class User_model extends CI_Model(){} so when you call it from controller use the lower case look like your model name user $this->load->model('user_model')
I think i've found the problem. I had some additional setter methods in my user controller which i've used all the time, didnt think they would be a problem.
public function __set($key, $value)
{
// Check to see that the requested attribute exists and then assign the value
if (property_exists($this, $key))
{
$this->$key = $value;
}
}
turns out I needed to take away one of the underscores as codeigniter doesn't like it.
public function _set($key, $value)
I should have really included the full class but I was trying to keep it as simple as possible!

Categories