How to use CodeIgniter Models

Remember:

  • Model classes in Codeigniter must be saved at application/models directory
  • Every Modal class must extend the Codeigniter’s base model class CI_Model
  • Class name should start with a capital letter, and the rest must be in lower case. If you want to have two or more words, you can separate those with an underscore. Eg: Mymodal, My_modal
  • When saving, class’s file name must match its class name definition and must be in all lower case letters. For an example, if your Modal class defined as Mymodal then it must be saved as application/models/mymodal.php. If your Modal class name defined as My_modal then it must be saved as application/models/my_modal.php.

Loading a Model

Before being able to use a model that you created, you have to load it from inside a Controller using the below statement syntax:

$this->load->model('model_name');

Using a Model

After a Model is loaded, you can use it from your controller methods using the below statement syntax:

$this->model_name->method();

--

--

Thinker, Scholar, Teacher, problem solver, and a self-made web developer

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store