CodeIgniter Models

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:

<?php 
    class Model_abc extends CI_Model 
    { 
       public function __construct()
               {  
          parent:: __construct(); 
       } 
    } 
 ?>

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:

$this->load->model(‘modelName’);

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:

$this->modelName-> modelFunction();

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.

<?php
 class Main extends CI_Model
 {
     public function fetchData()
     {
         return ['name' => 'Tutorials & Examples','topic'=> 'CodeIgniter Full Tutorial'  ]; 
     }
 }
 ?> 

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

<?php
 class Users extends CI_Controller 
 {
             public function index()
             {
         echo "This is my first Example of Model"; 
         echo "</br>";
         $this->load->model('Main'); // Here we have loaded the model file in the Controller.  
   $data=$this->Main->fetchData(); // here we call the fetchData method of the Main.php file
         print_r($data);
     }
 }
 ?> 

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

<?php
 class Main extends CI_Model
 {
     public function fetchData()
     {
         return [
             ['topic' => 'CodeIgniter', 'site' => 'Tutorial And Examples'],
             ['topic' => 'CakePHP' , 'site' => 'Tutorial And Examples']
         ];
     }
 }
 ?> 

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

User.php

<!DOCTYPE
html>
 <html>
 <head>
             <title style="color:red;"> Welcome to the Tutorials & Examples </title>
     </head> 
     <body>
     <h1 style="color:blue;" >Tutorials and Topic </h1>
     <table>
     <tr>
     <th> Topic Name </th>
     <th> Web Site </th> 
     </tr>
     <?php foreach($users as $row): ?>
     <tr>
     <td style="color:red;"> <?php echo $row['topic']; ?> </td> 
     <td style="color:green;"> <?php echo $row['site']; ?> </td>
     </tr>
     <?php endforeach; ?>
     </table>
     </body>
     </html> 

Step 4: Create Users.php in controller folder.

Users.php

<?php
 class Users extends CI_Controller {
             public function index()
             {    
         echo "This is a simple program using the model, View and the Controller";  
         echo "</br>";
         $this->load->model('Main');
         $data['users'] = $this->Main->fetchData(); 
         $this->load->view('users/user', $data);
     }
 } 
 ?> 

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