Skip to content

Commit

Permalink
docs: add notes for datetime ms/us in model and query builder
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Jul 29, 2024
1 parent 56c6c46 commit dea9d5b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
12 changes: 12 additions & 0 deletions user_guide_src/source/models/model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,18 @@ The datetime format is set in the ``dateFormat`` array of the
:ref:`database configuration <database-config-explanation-of-values>` in the
**app/Config/Database.php** file.

.. note::
When you set ``ms`` or ``us`` as a parameter, **Model** takes care of second's
fractional part of the Time. But **Query Builder** does not. So you still need
to use the ``format()`` method when you pass the Time to Query Builder's methods
like ``where()``:

.. literalinclude:: model/063.php
:lines: 2-

.. note:: Prior to v4.6.0, you cannot use ``ms`` or ``us`` as a parameter.
Because the second's fractional part of Time was lost due to bugs.

Custom Casting
==============

Expand Down
13 changes: 13 additions & 0 deletions user_guide_src/source/models/model/063.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

$model = model('SomeModel');

$now = \CodeIgniter\I18n\Time::now();

// The following code passes the microseconds to Query Builder.
$model->where('my_dt_field', $now->format('Y-m-d H:i:s.u'))->findAll();
// Generates: SELECT * FROM `my_table` WHERE `my_dt_field` = '2024-07-28 18:57:58.900326'

// But the following code loses the microseconds.
$model->where('my_dt_field', $now)->findAll();
// Generates: SELECT * FROM `my_table` WHERE `my_dt_field` = '2024-07-28 18:57:58'

0 comments on commit dea9d5b

Please sign in to comment.