Codeigniter Database Configuration

We will start by creating the tutorial project database. We will create a simple database for managing contact details. We will create a simple database with two (2) tables names pals and cities that they live in. The relationship between pals and cities is one-to-one with id in cities as the primary key and city_id as the foreign key in pals tables. Run the following scripts to create the database: Let’s now create the cities table

CodeIgniter Database Models

We will now create models for our database. The Model is the M part of the MVC. The model deals with data access, data manipulation, and business logic. In CodeIgniter, each model has to define the methods that it will support. Instead of repeating the same code in each model, we will take advantage of inheritance in object-oriented programming and create a parent model class that will define the basic methods that we wish our models to support. The table below shows the methods that we will define and how data will be accessed. The following image shows the class diagram and how Pals and Cities child models relate to the parent model BaseModel.

We will create two models as described in the above image Create a new class BaseModel in application/models/BaseModel.php Add the following code HERE,

protected $table = ”; defines a protected variable named table. This will be populated by the respective child class to specify which table our base model class methods should interact with. public function __construct() {…} defines the constructor method and executes the constructor method of the parent class CI_Model. get_all() {…} uses the database library and the value of the variable $table to run the SELECT query against our database. get_by_id($id) {…} defines the method for retrieving a single row from the database and accepts a parameter $id that should be of INT data type. get_where($where) {…} defines the get method that allows you to set a where clause. insert($data) {…} defines the insert method and accepts the array parameter $data that contains the values to be written to the database. update($id, $data) {…} defines the update method and accepts the array parameter $data that contains the values to be updated in the database. delete($id) {…} defines the delete method that accepts a parameter of $id that should be of data type INT.

now that we are done with the parent model class, lets create our Pals models Create a new file in application/models/Pals.php Add the following code HERE,

class Pals extends BaseModel {…} extends the parent model BaseModel and automatically makes all the methods defined in the BaseModel accessed to the child class. protected $table = ‘pals’; defines the table name associated with our parent model __construct() {…} initializes the parent constructor public function get_by_id($id) {…} overrides the get_by_id to provide custom implementation specific to Pals model. The query for get_by_id uses a join to retrieve the city name from cities table public function get_all() {…} overrides the get_all method to implement a join query between pals and cities table

Create a new file in application/models/Cities.php HERE, protected $table = ‘cities’; defines the model database table. As you can see from the above-given code, Inheritance saves us a lot of time when working with models in CodeIgniter. In the next section, we will learn Contacts Manager Controllers Now that we have created the models let’s create the controllers for our application. We will have two controllers namely Contacts and Cities Let’s start with cities Create a new file Cities.php in application/controllers directory Add the following code HERE, The above code implements all the methods needs to create, updated, delete and read rows from the database. Create another file Contacts.php in application/controllers Add the following code

Contacts Manager Views

We already looked at forms and validation in CodeIgniter in the previous tutorials. We will use the code that we developed in the previous tutorials. For completeness’ sake, we will reproduce the code we created in the previous tutorials. The views of our application will be as follows

You can download the code for the above views by clicking the link down below CodeIgniter Contacts Manager Views Download

Summary

In this tutorial, You will learn how to create models in CodeIgniter. We took advantage of inheritance in object-oriented programming to promote code reusability by creating a base model that implements the four major database operations of inserting, reading, updating and deleting. We have demonstrated the concepts using a practical application, and we shall continue to do so in the next tutorials by adding more functionality to the application.