Chapter 6. Models

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:

  • Adding, modifying, deleting, and searching the application database objects: Generally, this includes the database operations, but implementing the same operations and invoking external web services or APIs is not unusual at all.
  • Encapsulating the application logic: For example, the model can make data validations before storing a data object and can alert the calling application module about the problem.

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:

  • The CI model scope:
    • The model resource path
    • Loading a model
    • Using model methods
    • Connecting to a database
    • Business logic
  • Object Relational Mapping (ORM)
  • Example 1: a CRUD example
  • Example 2: a business logic example
  • Example 3: retrieving data from Facebook

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.

Scope of the CI model

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 resource path

The model files are located in the folder application/models/, in the pattern application/models/<MODEL_NAME>.php.

Loading a model

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.

  • If the model is used in a few of the controller's methods, it's recommended that you load the model in those methods. The scope of the model in that case is only in those methods project and will refer to application/models/mymodel.php.
  • If the model is used in most of the controller's methods, it's recommended that you load the model in the controller's constructor. In that case the scope of the model is in all the controller's methods project and will refer to application/models/mymodel.php.
    $this->load->model('mymodel'),

    It automatically loads a model mymodel for all the CI projects.

  • If the model is used in most of CI's project controllers, it is recommended that you autoload it in 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'),

Using model methods

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();

Connecting to a database

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

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.37.10