The model is responsible for handling the data of your application. Models receive input from Controllers and manage the application’s data, including inserting, updating, and retrieving information from the database.
Key Responsibilities of a Model:
- Managing Data: Models interact with the database by performing CRUD (Create, Read, Update, Delete) operations.
- Interaction with Controllers: Models receive input from Controllers and process the data accordingly.
Location of Models:
Model classes are located in the application/models/
directory and can be organized into sub-directories.
Creating a Basic Model in CodeIgniter 3
Basic Syntax for Creating a Model
The basic structure of a Model in CodeIgniter 3 is as follows:
class Model_name extends CI_Model { }
Rules:
- The filename must match the class name and should start with an uppercase letter.
- The class should extend the
CI_Model
class.
Example: Creating a Blog Model
Let’s walk through a practical example to understand how a Model works in CodeIgniter 3.
class Blog_model extends CI_Model { public function add() { $data = [ ‘title’ => ‘Title’, ‘description’ => ‘Description’ ]; // insert(‘table_name‘, ‘array_of_object’) $this->db->insert(‘blog_info’, $data); } public function get_blog_list() { $query = $this->db->get(‘blo_info’); return $query->result(); } }
Explanation:
- Defining the Model:
Blog_model
is defined with methodsadd()
andget_blog_list()
. - Inserting Data: The
add()
method inserts a new record into theblog_info
table. - Fetching Data: The
get_blog_list()
method retrieves all records from theblog_info
table.
Loading a Model in CodeIgniter 3
To use a Model in your Controller, you need to load it first. The load->model()
method is used to load a Model in CodeIgniter 3.
Basic Syntax:
$this->load->model('model_name');
Load a Model
A this->load->model() method is used to load a model.
The basic syntax is as follows:
$this->load->model('model_name');
Loading a Model from a Sub-directory
If your Model is inside a sub-directory, you need to specify the relative path from the models
directory.
For example, if your Model is located at application/models/user/Profile.php
, you would load it as follows:
$this->load->model('user/profile');
Accessing Model Methods
Once the Model is loaded, you can access its public methods using an object with the same name as the Model class:
$this->load->model('model_name'); $this->model_name->method();
Example: Loading and Using a Model in a Controller
Here’s an example of a Controller that loads the Blog_model
and uses its methods:
class Blog extends CI_Controller { public function index() { $this->load->model(‘blog_model’); $data[‘list’] = $this->blog_model->get_blog_list(); $this->load->view('about_us', $data); } }
Explanation:
- Loading the Model:
Blog_model
is loaded using$this->load->model('blog_model');
. - Using Model Methods: The
get_blog_list()
method is called to fetch the list of blogs. - Passing Data to the View: The fetched data is passed to the
about_us
view.