Models
A model is a class responsible for one database table. You state the table name and the list of fields in it, and the ready-made read and write methods come from the base class. The controller talks to the model and writes no SQL itself. Models live in the app/Models folder.
Creating a model
php spark make:model PostModelThe command creates the file app/Models/PostModel.php with many properties. These are enough to start:
// app/Models/PostModel.php
namespace App\Models;
use CodeIgniter\Model;
class PostModel extends Model
{
protected $table = 'posts';
protected $primaryKey = 'id';
protected $returnType = 'array';
protected $allowedFields = ['title', 'body', 'status'];
protected $useTimestamps = true;
}$table is the table name from the migration. $allowedFields lists the fields that may be written. $useTimestamps makes the model fill the created_at and updated_at columns itself.
The model in a controller
The model() function returns the model object:
// app/Controllers/Posts.php
namespace App\Controllers;
use App\Models\PostModel;
class Posts extends BaseController
{
public function index(): string
{
$posts = model(PostModel::class)->findAll();
return view('posts/index', ['posts' => $posts]);
}
}findAll() returns all rows of the table — each as an array. Open /posts: instead of posts written in the code, the list shows the records from the database added by the seeder.
One record
public function show(int $id): string
{
$post = model(PostModel::class)->find($id);
if ($post === null) {
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}
return view('posts/show', ['post' => $post]);
}find() looks a record up by its primary key and returns null if there is none. The address /posts/1 shows the first post, and /posts/999 shows the 404 page.
Reading methods
| Call | What it returns |
|---|---|
findAll() | All records |
findAll(10) | No more than ten records |
find(5) | The record with key 5 or null |
where('status', 1)->findAll() | Records matching the condition |
orderBy('id', 'DESC')->first() | The first record in sort order |
countAllResults() | The number of records |
Creating and updating
$posts = model(PostModel::class);
$id = $posts->insert([
'title' => 'New post',
'body' => 'The text of the post',
]);
$posts->update($id, ['status' => 1]);insert() adds a row and returns its identifier, update() changes the record with the given key. The save() method picks the action itself: if the data has an id key, the record is updated, otherwise created.
$posts->save(['id' => 5, 'title' => 'New title']);Deleting
model(PostModel::class)->delete(5);The row with key 5 is removed from the table. If the model sets protected $useSoftDeletes = true; and the table has a deleted_at column, the record is not erased but marked as deleted and stops appearing in queries.
Common mistake
A field missing from $allowedFields is silently dropped by the model: insert() passes without errors, yet the value is not saved. If a new column "does not save", check this list first. The protection exists so that a visitor cannot slip an extra field such as is_admin into a form.