Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

Say hello to redesigned Cloudways, an empowering Startup Program, enhanced Staging, a new Let’s Encrypt Wildcard SSL certificate feature, and more. GET STARTED

What is Codeigniter Active Records and How to Use It?

Updated on December 21, 2018

2 Min Read
Codeigniter Active Records
Reading Time: 2 minutes

Active Record is an important concept in PHP project development that removes many of the complexities of using databases in projects. In fact, Active Records is considered the Model in the MVC model.

Active Records is used to separate the business logic and data from the rest of the project and thus removes much of the potential sources of errors that could derail the development. Codeigniter Active Records is an easy-to-use database manipulation mechanism that saves a lot of time for the developers who can accomplish almost all database processing action in a few lines of code.

In this article, I will demonstrate how to use Codeigniter Active Records by creating a Controller, Model and a View.

Create the Database

I will start by creating the database that I will use for this tutorial. In the database.php (located in the application/config/ folder), add the following information:

You Might Also Like: Simple MVC Codeigniter CRUD Application

Now in the ‘activerecords’ database, create a table named ‘customer’ through the following query:

Configure the Model

Next, call the Model in main constructor method of the Controller file:

You Might Also Like: Display Data in Codeigniter JSON Format

Display the Data

To display the data extracted/processed by the Model (Codeigniter Active Records in this case), I will create a View file within the View folder. This View file will contain the following code:

At this point, it is important to define the Controller in the routes.php file. For the purpose of this tutorial, I will declare ‘home’ as the default controller. Check out the following code:

You Might Also Like: Pass Data From Controller to View in CodeIgniter

Next, pass the request in the index controller to handle active data records:

Configure the Model

Next, I will create the Model that will fetch the records from the ‘activerecord’ database. Here’s the code for the ‘datacustomer’ Model:

As you can see, using Codeigniter Active Records is very simple. The important fact to remember is that Active Records belong to the Model (data access layer/component) and not in Controller or View.

Here is a sample PHP code snippet that gets the results:

In the above code snippet, $this->db->selectis  used to select one or more columns from the table. Similarly, I have used $this->db->from to select one or more tables from the database. Finally, I have used $this->db->join to set up joins for tables.

Conclusion

In this short overview of Codeigniter Active Records and demonstrate how you can easily use it to abstract database handling and processing. You can use this tutorial as the basis for building a high performing Model for your Codeigniter project. In case of any issues, leave a comment below.

Share your opinion in the comment section. COMMENT NOW

Shahroze Nawaz

Shahroze is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. He’s always in search of new frameworks and methods to implement them. Besides his coding life, he loves movies and playing soccer with friends. You can email him at shahroze.nawaz@cloudways.com

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

Get Our Newsletter
Be the first to get the latest updates and tutorials.

THERE’S MORE TO READ.