CodeIgniter报错:调用未定义方法CourseModel::deletecour()求排查
Hey Kartik, let's break down why you're hitting that error and how to fix it. The "undefined method" message usually means CodeIgniter can't find the deletecour() method you're trying to call, which typically ties back to how your model is loaded or named. Here are the key checks to make:
1. Make sure your CourseModel is loaded in the controller
CodeIgniter won't automatically recognize your model unless you explicitly load it. You have a few options here:
Option A: Load the model in the controller's constructor
Add this to your controller's constructor so the model is available for all methods:
public function __construct() { parent::__construct(); $this->load->model('CourseModel'); }
Option B: Load the model directly in the deletecourse() method
If you only need the model for this specific method, load it at the start of deletecourse():
public function deletecourse(){ $this->load->model('CourseModel'); // Add this line first $id = $this->uri->segment(4); // ... rest of your code }
Option C: Auto-load the model globally
Edit application/config/autoload.php and add 'CourseModel' to the $autoload['model'] array to load it on every request:
$autoload['model'] = array('CourseModel');
2. Verify your model file and class naming matches CodeIgniter's conventions
CodeIgniter has strict naming rules for models:
- The model filename must be all lowercase:
coursemodel.php(save this inapplication/models/) - The model class name must start with an uppercase letter, followed by lowercase:
class CourseModel extends CI_Model
Double-check that your filename and class name follow this exactly—mismatches here will prevent CodeIgniter from finding the method, even if it exists in the code.
3. Double-check method spelling and case
While PHP is case-insensitive for method names, it's still good practice to ensure you're calling deletecour() exactly as it's defined in the model. Your code looks correct here, but a typo (like deleteCourse instead of deletecour) would throw this error.
Quick test to confirm
After making these checks, try adding a debug line right before you call deletecour() to see if the model is loaded:
var_dump($this->coursemodel); // Should show an object of type CourseModel $result=$this->coursemodel->deletecour($elete);
If this outputs null or a non-CourseModel object, your model isn't loading properly—go back to the naming/loading steps above.
Once you fix the model loading/naming issue, your delete functionality should work as expected!
内容的提问来源于stack exchange,提问作者Kartik Verma

