The Model is responsible for managing the data of the application. It receives user input from the Controller. It can contain functions to insert, update, and retrieve your application data.
CodeIgniter 4 models are an essential part of its MVC (Model-View-Controller) architecture. Models handle data-related logic, including database queries, data validation, and operations like inserting, updating, and deleting records.
Let’s explore how to use models in CodeIgniter 4 step by step.
Model classes reside in the Models directory. They can be nested within sub-directories.
The basic syntax for the model is:
<?php namespace App/Models; use CodeIgniter/Model; use CodeIgniter/Database/ConnectionInterface; class Model_name extends Model { protected $db; public function __construct(ConnectionInterface &$db) { $this->db =& $db; } } ?>
Setting Up the Environment
Before you start using models, ensure you have:
- A working CodeIgniter 4 setup.
- A database configured in the
.env
file.
Example .env
file configuration:
database.default.hostname = localhost
database.default.database = your_database
database.default.username = your_username
database.default.password = your_password
database.default.DBDriver = MySQLi
Creating a Model
Models in CodeIgniter 4 are typically created in the app/Models
directory. Each model is a PHP class that extends CodeIgniter/Model
.
Example: Creating a BlogModel
- Create a file named
BlogModel.php
in theapp/Models
directory. - Add the following code:
<?php namespace App/Models; use CodeIgniter/Model;
use CodeIgniter/Database/ConnectionInterface; class BlogModel extends Model { protected $db; public function __construct(ConnectionInterface &$db) { $this->db =& $db; } public function add() { $data = [ 'title' => 'Title', 'description' => 'Description' ]; return $this->db ->table('blog_info') ->insert($data); } public function get_blog_list() { return $this->db ->table('blog_info') ->where(["status" => "1"]) ->get() ->getResult(); } }?>
The file name must match the class name and the first letter of the Model class must be a capital letter.
How to use Model in a Controller?
You can use the model in controllers to interact with the database.
use App/Models/model_name;
Initialize constructor:
public function __construct() { $db = db_connect(); $this->model = new model_name($db); }
If your model is located in a sub-directory, include the relative path from your model’s directory. For example, if you have a model located at /app/Models/user/Profile.php you’ll load it using the:
use App/Models/user/Profile; public function __construct() { $db = db_connect(); $this->profileModel = new Profile($db); }
Once the model is loaded, you can access all the public methods using an object with the same name as your class:
$this->profileModel>method();
Here is an example of a controller, that loads a model, then serves a view:
<?php namespace App/Controllers; use App/Controllers/BaseController; use App/Models/BlogModel; class Blog extends BaseController { public function __construct() { $db = db_connect(); $this->blogModel = new BlogModel($db); } public function index() { $data[‘list’] = $this->blogModel>get_blog_list(); echo view('about_us', $data); } } ?>
NOTE: replace (/) with a backward slash in models and controllers
Methods Comparison in CodeIgniter 4 vs CodeIgniter 3
CodeIgniter 3 | CodeIgniter 4 |
---|---|
insert(); | insert(); |
delete(); | delete(); |
update(); | update(); |
row(); | getRow(); |
result(); | getResult(); |
CodeIgniter 4 models simplify database operations and integrate seamlessly into the MVC structure. With features like query builder support, data validation, and timestamps, models are powerful tools for developers. By following this tutorial, you can create and use models effectively in your projects.