Mastering CodeIgniter Framework: Fast, Lightweight Web Development
Course Title: Mastering CodeIgniter Framework: Fast, Lightweight Web Development Section Title: Database Integration with CodeIgniter Topic: Using CodeIgniter’s Active Record for database interactions
Introduction
In the previous topics, we have covered the basics of CodeIgniter's directory structure, routing, controllers, and views. In this topic, we will delve into the world of database interactions using CodeIgniter's Active Record. The Active Record pattern is a design pattern that abstracts the underlying database interactions, making it easier to write database-independent code. In this topic, we will explore how to use CodeIgniter's Active Record to interact with your database, and how to apply it in your real-world applications.
What is CodeIgniter's Active Record?
CodeIgniter's Active Record is a class that abstracts the underlying database interactions, providing a simple and easy-to-use interface to interact with your database. It allows you to perform CRUD (Create, Read, Update, Delete) operations on your database, without having to write raw SQL queries.
Key Concepts
Before we dive into the code, let's cover some key concepts:
- Model-View-Controller (MVC) pattern: CodeIgniter follows the MVC pattern, where the model is responsible for interacting with the database, the view is responsible for rendering the data, and the controller is responsible for handling the user input.
- Database interactions: CodeIgniter provides several ways to interact with your database, including using the database library, the Active Record, and the Query Builder.
- Query Builder: The Query Builder is a class that allows you to build database queries in a simple and easy-to-use way.
Using CodeIgniter’s Active Record
To use CodeIgniter's Active Record, you need to create a model class that extends the CI_Model
class. Here is an example:
// users_model.php
class Users_model extends CI_Model {
public function __construct() {
parent::__construct();
}
public function get_users() {
$this->db->select('*');
$this->db->from('users');
$query = $this->db->get();
return $query->result_array();
}
public function get_user($id) {
$this->db->select('*');
$this->db->from('users');
$this->db->where('id', $id);
$query = $this->db->get();
return $query->row_array();
}
public function update_user($id, $data) {
$this->db->where('id', $id);
$this->db->update('users', $data);
return $this->db->affected_rows();
}
public function delete_user($id) {
$this->db->where('id', $id);
$this->db->delete('users');
return $this->db->affected_rows();
}
}
In this example, we have created a Users_model
class that extends the CI_Model
class. The class has several methods that interact with the users
table, including get_users
, get_user
, update_user
, and delete_user
.
Practical Takeaways
Here are some practical takeaways from this topic:
- Use the Active Record to interact with your database: The Active Record provides a simple and easy-to-use interface to interact with your database. Use it to perform CRUD operations on your database.
- Use the Query Builder to build database queries: The Query Builder is a class that allows you to build database queries in a simple and easy-to-use way. Use it to build complex database queries.
- Use the model-view-controller pattern to separate concerns: The model-view-controller pattern is a design pattern that separates concerns between the model, view, and controller. Use it to separate concerns and make your code more maintainable.
Exercise
Create a new model class that interacts with a database table. Use the Active Record to perform CRUD operations on the table.
Additional Resources
- CodeIgniter documentation: Active Record
- CodeIgniter documentation: Query Builder
Leave a comment or ask for help: If you have any questions or need further clarification, please leave a comment below. I will be happy to help.
Images

Comments