Upgrade Models
Documentations
What has been changed
The CI4 model has much more functionality, including automatic database connection, basic CRUD, in-model validation, and automatic pagination.
Since namespaces has been added to CodeIgniter 4, the models must be changed to support namespaces.
Upgrade Guide
First, move all model files to the folder app/Models.
Add this line just after the opening php tag:
namespace App\Models;
.Below the
namespace App\Models;
line add this line:use CodeIgniter\Model;
.Replace
extends CI_Model
withextends Model
.Add the
protected $table
property and set the table name.Add the
protected $allowedFields
property and set the array of field names to allow to insert/update.Instead of CI3’s
$this->load->model('x');
, you would now use$this->x = new X();
, following namespaced conventions for your component. Alternatively, you can use themodel()
function:$this->x = model('X');
.
If you use sub-directories in your model structure you have to change the namespace according to that.
Example: You have a version 3 model located in application/models/users/user_contact.php the namespace has to be namespace App\Models\Users;
and the model path in the version 4 should look like this: app/Models/Users/UserContact.php
The new Model in CI4 has a lot of built-in methods. For example, the find($id)
method. With this you can find data where the primary key is equal to $id
.
Inserting data is also easier than before. In CI4 there is an insert($data)
method. You can optionally make use of all those built-in methods and migrate your code to the new methods.
You can find more information to those methods in Using CodeIgniter’s Model.
Code Example
CodeIgniter Version 3.x
Path: application/models:
<?php
class News_model extends CI_Model
{
public function set_news($title, $slug, $text)
{
$data = array(
'title' => $title,
'slug' => $slug,
'text' => $text,
);
return $this->db->insert('news', $data);
}
}
CodeIgniter Version 4.x
Path: app/Models:
<?php
namespace App\Models;
use CodeIgniter\Model;
class NewsModel extends Model
{
// Sets the table name.
protected $table = 'news';
public function setNews($title, $slug, $text)
{
$data = [
'title' => $title,
'slug' => $slug,
'text' => $text,
];
// Gets the Query Builder for the table, and calls `insert()`.
return $this->builder()->insert($data);
}
}
The above code is direct translation from CI3 to CI4. It uses Query Builder directly in the model. Note that when you use Query Builder directly, you cannot use features in CodeIgniter’s Model.
If you want to use CodeIgniter’s Model features, the code will be:
<?php
namespace App\Models;
use CodeIgniter\Model;
class NewsModel extends Model
{
// Sets the table name.
protected $table = 'news';
// Sets the field names to allow to insert/update.
protected $allowedFields = ['title', 'slug', 'text'];
public function setNews($title, $slug, $text)
{
$data = [
'title' => $title,
'slug' => $slug,
'text' => $text,
];
// Uses Model's`insert()` method.
return $this->insert($data);
}
}