This chapter covers the CI models, their role, and their usage with several code examples of web applications. The model is responsible for handling the database it stores and retrieves database objects used by the application from a database and contains the logic implemented by the application.
Much of the data that is part of the persistent state of the application (whether that persistent state is stored in files or databases) should reside in the model objects after the data is loaded into the application. Because the model objects represent knowledge and expertise related to a specific topic, they can be reused in the application.
The model represents the application data services and can serve the application logic (commonly referred to as business logic), as well. Usually, the model is responsible for the following operations:
The most common misuse of the CI database class is using it directly from the controller, view, or helper. A good practice is to develop the model classes to handle all the application database services.
Hence, all the other application modules can benefit, and reuse those models.
The CI models are special classes designed to handle databases or external information resources, such as Facebook (we will see an example of this in this chapter).
The CI models are the PHP classes that are designed to work with information in the database.
This chapter will primarily focus on the following topics:
We will begin by briefly reviewing the CI model scope and will proceed with several usage examples, covering different use cases that are combined in a real project.
The CI model provides services for all the application modules to access the application database(s) or external information resources in an OOP fashion. Typically, the model classes will contain functions that help us retrieve, insert, and update information in the database.
This section will focus on the CI model syntax and usage guidelines, as a preface to the following usage code examples.
The model files are located in the folder application/models/
, in the pattern application/models/<MODEL_NAME>.php
.
Loading a model can be done automatically or via the controller. More specifically, it can be done in a certain controller's constructor or any controller's method.
application/models/mymodel.php
.application/models/mymodel.php
.$this->load->model('mymodel'),
It automatically loads a model mymodel
for all the CI projects.
application/config/autoload.php
. In that case the scope of the model is in all the CI project and will refer to application/models/mymodel.php
.$autoload['model'] = array('users', 'mymodel'),
Once the CI model is loaded, we will access the model functions using an object with the model name as our class. The model's method is called for performing database operations, such as retrieving, inserting, and updating data from the database.
// Loading the model mymodel in the controller's method $this->load->model('mymodel'), // Calling the model's method my_function $this->mymodel->my_function();
For example, let's load the model users
and access its function get_users
.
// Loading the model class $this->load->model('usermodel'), // Calling the model to retrieve the users from the database $view_params['users'] = $this->usermodel->get_users();
For more information, refer to Chapter 2, Configurations and Naming Conventions.
In this example, we will connect manually to a database. The following settings are done in application/config/database.php
:
$config['hostname'] = '127.0.0.1'; $config['username'] = 'db_username'; $config['password'] = 'db_password'; $config['database'] = 'db_database'; $config['port'] = 'db_port'; $config['dbdriver'] = 'mysql'; $config['dbprefix'] = ''; $config['pconnect'] = TRUE; $config['db_debug'] = TRUE; $config['cache_on'] = FALSE; $config['cachedir'] = ''; $config['char_set'] = 'utf8'; $config['dbcollat'] = 'utf8_general_ci'; $config['swap_pre'] = ''; $config['autoinit'] = TRUE; $config['stricton'] = FALSE; // Loading the database with the configuration manually this->load->database($config);
Business logic is a set of validation rules and decision criteria defined for a certain information object topic or database object.
The model can apply business logic to the database and information objects that it handles.
In the case of a social network, the model layer would take care of tasks, such as saving user data, saving friend associations, storing and retrieving user photos, finding new friends for suggestions, and so on.
3.147.70.247