I want to use CodeIgniter 4’s Models’ ability to automatically set the created_at
, updated_at
, and deleted_at
fields in database tables.
For that, should I set those fields as TIMESTAMP
in MySQL or DATETIME
?
If I set them as TIMESTAMP
, I know that they will record the real-time regardless of the timezone, but the deleted_at
column cannot be null. On the other hand, if I set them as DATETIME
, I am not sure if CodeIgniter will store the time in ‘UTC’ or in the server’s time, which may not be ‘UTC’.
4
Answers
I would use
TIMESTAMP
, however, working with date-time objects is always a pain. It’s a matter of if you want it the data to be human readable in your database I think. Sincedeleted_at
cant beNULL
setting its default value to either super far in the past or super far in the future will allow you to run checks. ieif deleted_at != DISTANT_DATETIME run xyz
Did you ever think to use a Unix Time Stamp (epoch time), saving at
bigint
orvarchar
column definition?Using the CodeIgniter Model you can validate insert, update and delete actions to affect as desired.
When getting the record is more easy to convert using
DateTime
PHP class, like:But if you want to use
DATETIME
orTIMESTAMP
, my suggestion iscreated_at
: TIMESTAMP, this will storeupdated_at
: DATETIME, butyou must control when to save, and be aware that is going to get the
time zone reference
deleted_at
: DATETIME, same specs asupdated_at
Take this as advice:
Check this link
Background Information
Every time CodeIgniter runs, there are several base classes that are initialized automatically as part of the core framework. One of which is the
CodeIgniterCodeIgniter
system class.In this particular class, a default timezone used by all date/time functions on your server (
date_default_timezone_set
) and a default runtime locale (Locale::setDefault
) are set.Excerpt from the
CodeIgniterCodeIgniter
system classIf you look closely enough, the application’s ‘locale’ and ‘timezone’ settings are primarily based on what you defined in the
app/Config/App.php
configuration file or in the.env
environment variable file that resides at the root of your project.Excerpt from the
app/Config/App.php
configuration file.Default Locale and Application Timezone
So, to answer your inquiry,
Answer: That will depend on your app timezone setting in the
app/Config/App.php
configuration file or in the.env
environment variable file that resides at the root of your project. By default, the$appTimezone
property is set to ‘UTC’.Additional Explanation
With the background information out of the way, if you want to inform CodeIgniter 4 to automatically set the
created_at
andupdated_at
fields in addition to thedeleted_at
field for soft deleted records, you need to override the$useTimestamps
property on your Model and set it totrue
.$useTimestamps
These fields/columns (
created_at
,updated_at
&deleted_at
) are automatically filled/set when you call the following methods on your Model instance:->save(...)
.->insert(...)
.->insertBatch(...)
.->update(...)
.->updateBatch(...)
.->delete(...)
.Now, coming back to your question,
Response:
Firstly, at the time of writing this post, CodeIgniter 4 supports 3 formats by default, namely:
$dateFormat
With the linked resource above, CodeIgniter 4 expects the fields (
created_at
,updated_at
&deleted_at
) to have aDATETIME
data type. To supplement on this, one of CodeIgniter 4’s official packages called CodeIgniter Shield also usesDATETIME
as it’s preferred column data type for theses specific fields.Excerpt from Shield’s
Database/Migrations/2020-12-28-223112_create_auth_tables.php
fileCodeIgniter Settings, another official package also uses
DATETIME
as it’s preferred column data type for these specific fields.Excerpt from Settings’s
Database/Migrations/2021-07-04-041948_CreateSettingsTable.php
fileImportant notes:
deleted_at
field must be nullable. In addition, it is expected to have a column data type of eitherDATETIME
orINTEGER
.$useSoftDeletes
TIMESTAMP can only store dates up to ‘2038-01-19 03:14:07’ UTC. To avoid bugs or a database migration in the near future, you definitely want to use DATETIME.
Source: https://dev.mysql.com/doc/refman/8.0/en/datetime.html