Models are the classes that deals with backend operations. It is used to fetch the data from the database and send it to the Controller. All the database related information provided inside the CI_Model. For example, suppose you have created a web application using the CodeIgniter framework to manage the users. or this, you have to require a model class through that you can perform the update, read, insert, and delete.

Syntax:

There are a few essential points listed

  • You have to create a new folder inside the application/model’s folder, that is present in the CodeIgniter folder of your server.
  • The first letter of the model class must be in the uppercase. The model class name and model file name must be same.

For example, suppose Model_abc, Modelname, and ModelName are class names, the file name of these class name will be Model_abc.php, Modelname.php, and ModelName.php.

  • A model class extends a codeignitor model file to access all predefined functions of the original file that can be inherited to the newly created file.

Loading a Model

A CodeIgniter provides a load() method for loading the model in the Controller’s file. In this method, a model file is rendered to the Controller’s action.

Syntax:

The above syntax represents that a modelName is the name of your model class that will be loaded to the model() method. So, we don’t require to use the .php extension in the model file.

After loading the model file in the controller, now you have to load the function of the model’s class.

Syntax:

Similarly, you can create multiple model classes and their function in the model folder. You can call the models and their function in the Controller class.

Creating a Model file

Here we will understand how we can create a model class inside the model folder of the application in the CodeIgniter. There are following steps to create a model file in CodeIgniter:

  1. Extend the application folder and select the models folder.
CodeIgniter Models

2. Right-click on the models folder, click on the New File menu.

CodeIgniter Models

3. Provide the file name. We have provided the file name Main.php.

CodeIgniter Models

4. Write the following code in Main.php file.

Create a controller file in the folder application/controller. We have created a controller file with the name Users.php.

Open the browser and execute the URL localhost/CodeIniter-3.1.11/index.php/Users/. It shows the output, as shown below.

CodeIgniter Models

Create a program using MVC

Let’s create a program by using the following steps, in CodeIgniter framework that follows MVC design pattern.

Step 1: Create three folders model, view, and controller inside the application folder of the project.

Step 2: Create Main.php in models folder.

Main.php

Step 3: Create User.php in the view folder.

User.php

Step 4: Create Users.php in controller folder.

Users.php

After creating all the files open the browser and invoke the URL localhost/CodeIgniter-3.1.11/index.php/Users. It shows the output as shown below.

CodeIgniter Models

Pin It on Pinterest

Share This