How to create model in CodeIgnitor?

blank (2)

Hello Friends,

Today we will discuss model creation in CodeIgnitor.

First of all, we will create a model file in \ci\application\models\ folder.
Suppose model file will be named as User_model.php and the class name will be User_model and after that create the constructor of the class who will be call the parent class constructor.
Now We will create a method for deletion of the system user from the system_users table in the database.Code will be given below:

After creating the model, Now we will open the autoload.php file in \ci\application\config\autoload.php and make entry in $autoload[‘model’] array for the above created model.Code will be display as below:

After this code written for User model, this model will be available for the whole application.

Now I will create a Controller file in the \ci\application\controllers\ folder and named it User.php file
Controller Class name will be User class and it will extends from CI_Controller and the code will be written as below:

Now on the Line no. 36, the deleteuser() model is called and it takes user_id as a parameter.

Now you can create your model and can call in your controller in a easy way.

Leave a Comment

Your email address will not be published. Required fields are marked *

Optimization WordPress Plugins & Solutions by W3 EDGE