Created_at and updated_at columns and their definition in CodeIgniter 4 models

- Andrés Cruz

En español
Created_at and updated_at columns and their definition in CodeIgniter 4 models

That we have the advantage of defining a field that is automatically filled with the current date at the time the record is created and more interesting, that when we edit that record, the current date is defined, it is something extremely simple in frameworks like Laravel and that in CodeIgniter 4, we can also use in a simple way. We need to define about 3 attributes in our model:

  1. $useTimestamps - Boolean that indicates if you are going to enable the timestamp for creation and update.
  2. $createdField - Define the name of the column of the creation field.
  3. $updatedField - Define the name of the update field column.

For example, in the following model, we define the corresponding properties along with the 3 noted above:

<?php
namespace App\Models; 
use CodeIgniter\Model;
 
class ProductsControlModel extends Model
{
    protected $table = 'products_control';
    protected $primaryKey = 'id';
    protected $allowedFields = ['product_id', 'type','count', 'created_at', 'updated_at'];
    protected $useTimestamps = true;
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.