CodeIgniter Basic Tutorial
Performance & Utilities
CodeIgniter Advanced
In CodeIgniter, a Model is a PHP class that interacts with your database.
It is responsible for:
Think of the Model as the database handler in your application.
It does not deal with HTML or user interface — only data logic.
Models are stored in:
app/Models/
Each model usually corresponds to a specific database table.
Example: ProductModel for products
table
File: app/Models/ProductModel.php
File: app/Controllers/Product.php
Method |
Description |
Example |
---|---|---|
findAll() |
Get all rows |
$model->findAll(); |
find($id) |
Get a single row by primary key |
$model->find(1); |
where() |
Add WHERE condition |
$model->where('price >', 50)->findAll(); |
save() |
Insert or update |
Insert or update |
insert() |
Insert new row |
$model->insert($data); |
update() |
Update a row |
$model->update($id, $data); |
delete() |
Delete a row |
$model->delete($id); |
Flow:
Model → Controller → View
Example:
// Controller $productModel = new ProductModel(); $data['products'] = $productModel->findAll(); return view('products_list', $data);
$allowedFields
.