Javatpoint Logo
Javatpoint Logo


What is a Model

In any application you need to call a function to retrieve some information from the database. Models responsibility is to handle all data logic and representation and load data in the views. It is stored in application/models.

Codelginator Models 1

Look at the above snapshot, this is the basic structure of a model file.

Here, ModelName is the name of your model file. Remember, class first letter must be in uppercase letter followed by other lowercase letters and it should be same as your file name. It extends the base CodeIgniter Model so that all the built in methods of parent Model file gets inherited to the newly created file.

Model file name will be saved with an uppercase letter in the folder application/models. For example, if the above Modelname is your class then your file name will be ModelName.php

Loading a Model

Models are loaded in the conntroller's file with the following code,

If in case your model file is located in sub-directory of model folder, then you have to mention the full path. For example, if your file location is application/controller/models/project/ModelName. Then, your file will be loaded as shown below,

Connecting Models to Database

Loading a model doesn't mean it will automatically connect to your database. There are different methods to connect a database.

.Auto-connect feature will automatically load your database with every page load. To enable it, add word 'database' in the array library in autoload.php file.

Codelginator Models 2

Manually connect database by adding this code in the page where needed.

Next TopicViews

Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials

B.Tech / MCA